gpt4 book ai didi

ios - Kal 数据源代表

转载 作者:行者123 更新时间:2023-11-28 20:24:05 25 4
gpt4 key购买 nike

我正在使用 Kal https://github.com/klazuka/Kal实现一个日历。我正在使用 Storyboard,所以我将 KalViewController 作为子项添加到容器 View Controller 中。

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];

我使用外部对象作为数据源委托(delegate),但在通过网络服务从我的后端获取数据后,我不知道如何调用委托(delegate)来使用获取的数据加载日历:

@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;

}

最佳答案

首先,我发现您在使用 KalViewController 之后分配它很奇怪,我会按照相反的顺序进行分配:

kal = [[KalViewController alloc] init];
kal.delegate = self;
kal.dataSource = dataSource;

那你为什么不直接打电话呢?:

[kal reloadData];

关于ios - Kal 数据源代表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14748922/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com