2011-03-27 3 views
0

데이터가 변경된 후 UITableView에있는 셀을 어떻게 다시로드 할 수 있는지 조금 혼란 스럽습니다. 특히 내가 가지고있는 혼란은 새로운 데이터가 현재 표시되는 것보다 더 많거나 적은 섹션을 가질 수 있다는 것입니다. reloadSections:withRowAnimation: 방법이 있다는 것을 알고 있지만 1 : 1 교체가 필요합니다. 그 곳에서 또는 없을 수도 있습니다.얼마나 많은 섹션이 있을지 모를 때 어떻게 UITableView의 데이터를 다시로드합니까?

나는 단지 UITableView에게 모든 것을 스크랩하고 처음으로 다시로드하라고 말하고 싶습니다. 나는 누군가가 이것에 대해 밝히고있는 것에 감사 할 것입니다.

미리 감사드립니다. 다음은 UPDATE

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"]; 

    NSLog(@"%@", cell); 

    if (cell == nil) { 
     NSLog(@"%d; %d", indexPath.section, vehicle.inventoryCategoriesCount); 

     if (indexPath.section < vehicle.inventoryCategoriesCount) { 
      NSLog(@"Grouped cell"); 

      cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"Cell"] autorelease]; 

      NSString *model = [[[vehicle.inventory filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"category == %@", [vehicle.inventoryCategories objectAtIndex:indexPath.section]]] valueForKeyPath:@"@distinctUnionOfObjects.model"] objectAtIndex:indexPath.row]; 

      cell.textLabel.text = model; 
      cell.detailTextLabel.text = [NSString stringWithFormat:@"%d", [[vehicle.inventory filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"model == %@", model]] count]]; 
      cell.selectionStyle = UITableViewCellSelectionStyleNone; 
     } else { 
      NSLog(@"Remove cell"); 

      cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"] autorelease]; 

      cell.textLabel.text = @"Remove an Item"; 
      cell.textLabel.textColor = [UIColor redColor]; 
      cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 
     } 
    } 

    NSLog(@"Adding a cell"); 

    return cell; 
} 

답변

1

당신은에 reloadData를 호출 할 수 있습니다 ... 나는라고 reloadData 후 dequeueing하지 세포로 문제를 발견하는 사용하고 코드입니다 테이블 뷰.

[myTableView reloadData]; 

문서

은 여기에 있습니다 : http://developer.apple.com/library/ios/#documentation/uikit/reference/UITableView_Class/Reference/Reference.html

+0

흠, 내가 데이터를 얻을 그것을 구문 분석하고 배열을 업데이트 한 후에 내가 그 메소드를 호출하고 볼 수 있지만, 그것의 세포를 바꾸지 않아. 테이블을 채우는'MSMutableArray'를 가지고 있고 그 객체는'category'와'model'이라는 속성을 가지고 있습니다. 'category'는 섹션이고,'model'은 객체를 그룹화하고 각/모델의 수를 표시하는 데 사용됩니다. 지금, 나는 "add"코드를 만들고 있는데, 변화하고있는 모델 그룹이 그것의 카운트를 업데이트하지 않는다는 것만 제외하면 작동해야한다. 다소 실망 스럽다 ... – Gup3rSuR4c

+0

실제로, 더 많은 테스트를 할 때, - (UITableViewCell *) tableView : (UITableView *) tableView cellForRowAtIndexPath : (NSIndexPath *) indexPath'가 호출됩니다. 이전 생성에서 셀을 대기열에 추가하지 않습니다. 그래서 내가 잘못하고있는거야? – Gup3rSuR4c

+0

'reloadData'를 호출하면 모든 데이터 소스 메소드 (numberOfSections, cellForRowAtIndexPath 등)가 다시 호출되므로 올바르게 업데이트 된 값을 반환해야합니다. 그렇다면 여기서 재사용 가능한 셀을 다시 대기열에 추가해야합니다. – jhabbott

0

당신은 핵심 데이터 및 가져온 결과 컨트롤러를 사용하는 경우, 그것은 매우 간단합니다. 실제로, 테이블 뷰가있는 샘플 코어 데이터 프로젝트를 빌드하면이 사실을 알 수 있다고 생각합니다.

두 가지 다른 방법이 떠 올랐습니다. 컨트롤러 didChangeSection이 아마도 당신이 찾고있는 것일 것입니다. 첫 번째는 fetchedresultscontroller 델리게이트 프로토콜의 일부입니다. 두 번째는 tableviewdatasource 프로토콜의 일부입니다.

- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo 
     atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type { 

    switch(type) { 
     case NSFetchedResultsChangeInsert: 
     [self.noteTableView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade]; 
     break; 

    case NSFetchedResultsChangeDelete: 
     [self.noteTableView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade]; 
     break; 
} 

}

다른입니다

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
return [[fetchedResultsController sections] count]; 

}

관련 문제