Kal 数据源代表
Posted
技术标签:
【中文标题】Kal 数据源代表【英文标题】:Kal datasource delegates 【发布时间】:2013-02-07 10:32:00 【问题描述】:我正在使用 Kal https://github.com/klazuka/Kal 来实现日历。我正在使用情节提要,因此我将 KalViewController 作为子项添加到容器视图控制器中。
kal.delegate = self;
kal.dataSource = dataSource;
dataSource = [[CalendarDataSourceDelegate alloc] init];
kal = [[KalViewController alloc] init];
[self addChildViewController:kal];
[kal didMoveToParentViewController:self];
[self.calendarioView addSubview:kal.view];
[clickEvent insertPrintAdd:zona_competicion];
我使用外部对象作为数据源委托,但是在通过 web 服务从我的后端获取数据后,我不知道如何调用委托来加载带有获取数据的日历:
@implementation CalendarDataSourceDelegate
- (id)init
if ((self = [super init]))
items = [[NSMutableArray alloc] init];
matches = [[NSMutableArray alloc] init];
buffer = [[NSMutableData alloc] init];
[self fetchHolidays];
return self;
- (eventMatch *)eventAtIndexPath:(NSIndexPath *)indexPath
return [items objectAtIndex:indexPath.row];
#pragma mark UITableViewDataSource protocol conformance
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
static NSString *identifier = @"MyCell";
eventNoAddedCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (!cell)
cell = [[eventNoAddedCell alloc] initWithStyle:UITableViewCellSelectionStyleNone reuseIdentifier:identifier];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.imageView.contentMode = UIViewContentModeScaleAspectFill;
eventMatch *event = [self eventAtIndexPath:indexPath];
cell.escudoLocal.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@.png",event.equipoLocal ]];
cell.escudoVis.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@.png",event.equipoVisitante ]];
cell.equipoLocal.text = event.equipoLocal;
cell.equipoVisitante.text = event.equipoVisitante;
cell.competicion.text = event.competicion;
cell.jornada.text = event.jornada;
cell.hora.text = event.hora;
cell.canalesTV.text = event.canalesTV;
[self fetchHolidays];
return cell;
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
return [items count];
#pragma mark Fetch from the internet
- (void)fetchHolidays
NSString *urlStr = [NSString stringWithFormat:@"http://backend.exular.net/contenido/webservices/xlr_ws_calendario.php?idp=105"];
dataReady = NO;
[matches removeAllObjects];
conn = [NSURLConnection connectionWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlStr]] delegate:self];
[conn start];
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
[buffer setLength:0];
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
[buffer appendData:data];
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
NSError *error=nil;
NSDictionary *tempDic = [NSJSONSerialization JSONObjectWithData:buffer options:kNilOptions error:&error];
NSArray *array = [tempDic valueForKey:@"nodes"];
NSDateFormatter *fmt = [[NSDateFormatter alloc] init] ;
[fmt setDateFormat:@"dd/MM/yyyy"];
for (NSDictionary *jornada in array)
NSDate *d = [fmt dateFromString:[jornada objectForKey:@"fecha"]];
[matches addObject:[eventMatch equipoVisitante:[jornada valueForKey:@"id_visitante"] equipoLocal:[jornada valueForKey:@"id_local"] resultadoVisitante:[jornada valueForKey:@"res_visitante"] resultadoLocal:[jornada valueForKey:@"res_local"] canalesTV:[jornada valueForKey:@"cadenas"] date:d time:[jornada valueForKey:@"hora"] jornada:[jornada valueForKey:@"jornada"] competicion:[jornada valueForKey:@"competicion"]]];
dataReady = YES;
[callback loadedDataSource:self];
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
NSLog(@"HolidaysCalendarDataSource connection failure: %@", error);
#pragma mark KalDataSource protocol conformance
- (void)presentingDatesFrom:(NSDate *)fromDate to:(NSDate *)toDate delegate:(id<KalDataSourceCallbacks>)delegate
/*
* In this example, I load the entire dataset in one HTTP request, so the date range that is
* being presented is irrelevant. So all I need to do is make sure that the data is loaded
* the first time and that I always issue the callback to complete the asynchronous request
* (even in the trivial case where we are responding synchronously).
*/
if (dataReady)
[callback loadedDataSource:self];
return;
callback = delegate;
[self fetchHolidays];
- (NSArray *)markedDatesFrom:(NSDate *)fromDate to:(NSDate *)toDate
if (!dataReady)
return [NSArray array];
return [[self matchesFrom:fromDate to:toDate] valueForKeyPath:@"date"];
- (void)loadItemsFromDate:(NSDate *)fromDate toDate:(NSDate *)toDate
if (!dataReady)
return;
[items addObjectsFromArray:[self matchesFrom:fromDate to:toDate]];
- (void)removeAllItems
[items removeAllObjects];
#pragma mark -
- (NSArray *)matchesFrom:(NSDate *)fromDate to:(NSDate *)toDate
NSMutableArray *matchesUpdate = [NSMutableArray array];
for (eventMatch *match in matches)
if (IsDateBetweenInclusive(match.date, fromDate, toDate))
[matchesUpdate addObject:match];
return matches;
- (void)dealloc
matches=nil;
【问题讨论】:
【参考方案1】:首先,我觉得奇怪的是你在使用 KalViewController 后分配它,我会按照相反的顺序来做:
kal = [[KalViewController alloc] init];
kal.delegate = self;
kal.dataSource = dataSource;
那你为什么不直接打电话呢?:
[kal reloadData];
【讨论】:
以上是关于Kal 数据源代表的主要内容,如果未能解决你的问题,请参考以下文章
Calendar Kal:如何动态刷新 tableview?
(kal) 日历/tableview 视图层次结构在标签栏中中断
如何正确地将 Kal 框架添加到我的 iPhone 项目中?