我正在使用一个获取的结果 Controller ,它似乎想在我的 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 个部分?
最佳答案
为了使您的获取结果 Controller 避免返回分段对象,您需要按如下方式正确初始化它:
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:request managedObjectContext:managedObjectContext sectionNameKeyPath:nil cacheName:@"Root"];
这里不会有任何节,因为您传递了 nil
作为 sectionNameKeyPath
的参数。那么,您的 tableView:numberOfRowsInSection
方法是正确的。
关于iPhone:获取结果 Controller ,不要使用 UITableView 中的部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2983129/