繁体   English   中英

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

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

我对如何更改数据后如何在UITableView重新加载单元格有些困惑。 特别是,我一直感到困惑的是,新数据的部分可能比当前显示的部分更多或更少。 我知道这里有reloadSections:withRowAnimation:方法,但是这需要1:1替换,而我可能有也可能没有。

我只想告诉UITableView所有内容并像第一次一样重新加载。 我希望有人对此有所启发。

提前致谢!

更新

这是我正在使用的代码,我发现问题是调用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;
}

您可以在表视图上调用reloadData

[myTableView reloadData];

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

如果您使用核心数据和获取的结果控制器,则非常简单。 实际上,我相信如果您构建一个带有表视图的示例核心数据项目,就可以看到这一点。

我想到两种不同的方法:控制器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];

}

暂无
暂无

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

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