Kal数据源代表

时间:2013-02-07 10:32:01

标签: ios objective-c

我在Kal https://github.com/klazuka/Kal中使用它来实现日历。我正在使用storyboard,所以我将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];

我使用外部对象作为数据源委托,但是在通过webservice从后端获取数据之后,我不知道如何调用委托来加载带有获取数据的日历:

@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 个答案:

答案 0 :(得分:3)

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

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

然后你为什么不打电话?:

[kal reloadData];