4

我正在使用一个获取的结果控制器,它似乎想要使用我的 UITableView 中的部分进行设置。好吧,在这种情况下,我不想使用部分。其中,将 numberOfSectionsInTableView: 的值设置为 1 很容易。但是,现在我不确定如何获取 numberOfRowsInSection: 以将所有单元格返回到一个部分中。

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    id <NSFetchedResultsSectionInfo> sectionInfo = [[fetchedResultsController sections] objectAtIndex:section];
    return [sectionInfo numberOfObjects];
}

我怎样才能使该方法返回所有单元格,因为我只想使用 1 个部分?

4

2 回答 2

3

为了让您的获取结果控制器避免在部分中返回对象,您需要正确初始化它,如下所示:

NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:request managedObjectContext:managedObjectContext sectionNameKeyPath:nil cacheName:@"Root"];

这里不会有任何部分,因为您nil作为 的参数传递sectionNameKeyPath。那么,你的tableView:numberOfRowsInSection方法是正确的。

于 2010-06-06T07:06:13.017 回答
1

即使您不想使用部分,并使用 sectionNameKeyPath=nil 初始化 fetchedResultsController,这个 fetchedResultsController 仍然填充有 ONE 部分及其所有项目。这就是 Apple 实现 fetchedResultsController 的方式。

因此,在您的情况下,您在索引 0 处有 1 个部分,在此部分中,您拥有从索引 0 到 numberOfRowsInSection-1 的所有项目。

希望能帮助到你。

于 2010-06-06T13:19:42.727 回答