2013-06-18 2 views
0

사용자 지정 UITableViewCells가있는 UITableView가 있습니다. 사용자 지정 UITableViewCells는 NSDictionary를 사용하여 레이블과 이미지를 채 웁니다. 내 문제는, NSArray 각 셀 (NSDictionaries) 내부 정보를 서버에서 반환했습니다. 모든 셀은 UITableViewCell과 동일한 서브 클래스이지만 모든 셀이 다른 배열을 사용해야합니다.사용자 지정 셀이있는 UITableView

다음
//I use this method to create the cell, and build it using the NSDictionary passed to it 
+ (CategoryCell *)generateCategoryCellWithInfo:(NSDictionary *)dict; 
{ 
    CategoryCell *cell; 
    NSArray *xib = [[NSBundle mainBundle] loadNibNamed:@"CategoryCell" owner:self options:nil]; 
    cell = [xib objectAtIndex:0]; 

    if (dict != nil) 
    { 
     //build the cell 
     cell.videoTitle.font = [UIFont fontWithName:@"FuturaStd-CondensedBold" size:17.0f]; 
     cell.videoTitle.text = [dict objectForKey:@"title"]; 

     dispatch_async(dispatch_get_global_queue(0, 0), ^(void){ 
      NSData *fullImageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:[dict objectForKey:@"fullImage"]]]; 
      if (!fullImageData) 
      { 
       return; 
      } 
      else 
      { 
       dispatch_async(dispatch_get_main_queue(), ^(void){ 
        [cell.contentButton setImage:[UIImage imageWithData:fullImageData] forState:UIControlStateNormal]; 
       }); 
      } 
     }); 

     cell.numberOfViewsLabel.text = [NSString stringWithFormat:@"%@ views", [dict objectForKey:@"views"]]; 
    } 
    else 
    { 
     //set placeholders 
     cell.numberOfViewsLabel.text = @"1200 people viewed this"; 
     cell.numberOfLikesLabel.text = @"20 people liked this"; 
     [cell.contentButton setImage:[UIImage imageNamed:@"[email protected]"] forState:UIControlStateNormal]; 
     cell.videoTitle.text = @"A Baby Dances in its Car Seat or Something"; 
    } 

    return cell; 
} 

는 세포가 지금

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CategoryCellIdentifier = @"categoryCell"; 
    CategoryCell *cell = (CategoryCell *)[tableView dequeueReusableCellWithIdentifier:CategoryCellIdentifier]; 

    if (!cell) 
    { 
     cell = [CategoryCell generateCategoryCellWithInfo:[itemsArray objectAtIndex:0]]; 
     cell.delegate = self; 
    } 

    return cell; 
} 

을 만들어 난 그냥있는 NSArray itemsArray에 처음있는 NSDictionary를 사용하고,하지만 각 셀이 전달 갖고 싶어 : 여기에 몇 가지 코드입니다 itemsArray 내의 다른 NSDictionary. 누군가 나를 도울 수 있습니까? 당신이 당신의 테이블에 0 섹션을 사용하는 경우

답변

1

:

그냥 tableView:cellForRowAtIndexPath: 방법 [itemsArray objectAtIndex: indexPath.row]-[itemsArray objectAtIndex: 0]을 변경합니다. 당신이 당신의 표가 전역 변수 NSInteger indexCountForItemsArray;을 추가하고 매번 tableView:cellForRowAtIndexPath:를 증가 제안에 하나 이상의 section가 호출 가지고있는 경우에

. 두 경우 모두에서

나는 tableViewController에 -(void)setInfo:(NSDictionary)dict 같은 공공 방법을 추가하고 넣어 것이라고 수업 방법 +(CategoryCell*)generateCategoryCellWithInfo:(NSDictionary *)dict처럼 같은/비슷한 코드 :

- (void) setInfo : (NSDictionary*) dict { 
    if (dict != nil) { 
     //build the cell (your known code here) 
    } 
} 

- (UITableViewCell*) tableView : (UITableView*) tableView 
     cellForRowAtIndexPath : (NSIndexPath*) indexPath 
{ 
    static NSString *CategoryCellIdentifier = @"categoryCell"; 
    CategoryCell *cell = (CategoryCell *)[tableView dequeueReusableCellWithIdentifier:CategoryCellIdentifier]; 

    if (cell == nil) { 
     cell = [CategoryCell generateCategoryCellWithInfo : [itemsArray objectAtIndex: indexCountForItemsArray]]; 
     cell.delegate = self; 
    } else { 
     [cell setInfo : [itemsArray objectAtIndex: indexCountForItemsArray]]; 
    } 

    self.indexCountForItemsArray += 1; 
    return cell; 
} 

희망 이것은 당신을 도와줍니다.

+0

완벽하게 작동합니다! 도와 주셔서 감사합니다. –

+0

다행, 천만에. – anneblue

관련 문제