简体   繁体   English

当我不知道会有多少节时,如何重新加载UITableView的数据?

[英]How do I reload a UITableView's data when I don't know how many sections there will be?

I'm a bit confused on how I can reload the cells in a UITableView after it's data has been changed. 我对如何更改数据后如何在UITableView重新加载单元格有些困惑。 In particular, the confusion I'm having is that the new data could have more or less sections than what is currently on display. 特别是,我一直感到困惑的是,新数据的部分可能比当前显示的部分更多或更少。 I know there's the reloadSections:withRowAnimation: method, but that requires a 1:1 replacement, where I may or may not have that. 我知道这里有reloadSections:withRowAnimation:方法,但是这需要1:1替换,而我可能有也可能没有。

I just want to tell the UITableView to scrap everything and reload as if for the first time. 我只想告诉UITableView所有内容并像第一次一样重新加载。 I'd appreciate someone shedding some light on this. 我希望有人对此有所启发。

Thanks in advance! 提前致谢!

UPDATE 更新

Here's the code I'm using which I've found the issue to be the cells not dequeueing after reloadData is called... 这是我正在使用的代码,我发现问题是调用reloadData后单元不出队...

- (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;
}

You can call reloadData on the table view. 您可以在表视图上调用reloadData

[myTableView reloadData];

The documentation is here: http://developer.apple.com/library/ios/#documentation/uikit/reference/UITableView_Class/Reference/Reference.html 该文档位于: http : //developer.apple.com/library/ios/#documentation/uikit/reference/UITableView_Class/Reference/Reference.html

If you are using core data and the fetched results controller, it's very straightforward. 如果您使用核心数据和获取的结果控制器,则非常简单。 In fact, I believe you can see this if you build a sample core data project with a table view in it. 实际上,我相信如果您构建一个带有表视图的示例核心数据项目,就可以看到这一点。

Two different methods come to mind: controller didChangeSection is probably what you're looking for. 我想到两种不同的方法:控制器didChangeSection可能正是您想要的。 The first is part of the fetchedresultscontroller delegate protocol. 第一个是fetchedresultscontroller委托协议的一部分。 The second is part of the tableviewdatasource protocol. 第二个是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;
}

} }

the other is: 另一个是:

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

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM