2013-02-07 93 views
0

我使用Kal https://github.com/klazuka/Kal來實現日曆。我使用的是故事板,所以我將KalViewController作爲子視圖添加到容器視圖控制器。Kal datasource代表

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; 

    } 

回答

3

第一,我覺得奇怪的是,你使用它後你的頁頭KalViewController,我這樣做只是以相反的順序:

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

然後你爲什麼不只是調用?:

[kal reloadData];