我正在使用一个获取的结果控制器,它似乎想要使用我的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节?
答案 0 :(得分:3)
为了让你的获取结果控制器避免在部分中返回对象,你需要按如下方式正确初始化它:
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:request managedObjectContext:managedObjectContext sectionNameKeyPath:nil cacheName:@"Root"];
此处没有任何部分,因为您将nil
作为sectionNameKeyPath
的参数传递。然后,您的tableView:numberOfRowsInSection
方法是正确的。
答案 1 :(得分:1)
即使你不想使用section,并使用sectionNameKeyPath = nil初始化fetchedResultsController,这个fetchedResultsController仍然填充了一个部分及其所有项目。这就是Apple实现fetchedResultsController的方式。
因此,在您的情况下,您在索引0处有1个部分,并且在此部分中,您拥有所有项目,从索引0开始到numberOfRowsInSection-1。
希望它有所帮助。