获取的结果控制器 returns 部分数 = 0
Fetched Results Controller returns Number of Sections = 0
我在 UITableViewController
中使用 NSFetchedResultsController
。
不幸的是,[self.fetchedResultsController.sections count]
return 的值为 0,这是不正确的。它应该 return 1 部分:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
NSInteger numberOfSections = [self.fetchedResultsController.sections count];
return numberOfSections;
}
这是我用来实例化 NSFetchedResultsController
并加载结果的代码:
@interface SearchQueryViewController ()
@property (strong, nonatomic) NSFetchedResultsController *fetchedResultsController;
@property Search *selectedSearch;
@end
@implementation SearchQueryViewController
- (void)viewWillAppear:(BOOL)animated
{
if (!self.managedDocument) {
[[MyDocumentHandler sharedDocumentHandler] performWithDocument:^(UIManagedDocument *document) {
self.managedDocument = document;
// Uncommenting the following code lines does not change the outcome:
/*self.fetchedResultsController = nil;
[self.tableView reloadData];*/
}];
}
[super viewWillAppear:animated];
}
# pragma Fetched Results Controller
- (NSFetchedResultsController *)fetchedResultsController
{
if (_fetchedResultsController) return _fetchedResultsController;
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Search"];
request.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"query" ascending:YES selector:@selector(localizedCaseInsensitiveCompare:)]];
self.fetchedResultsController = [[NSFetchedResultsController alloc]
initWithFetchRequest:request managedObjectContext:self.managedDocument.managedObjectContext
sectionNameKeyPath:nil cacheName:nil];
return _fetchedResultsController;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
NSInteger numberOfSections = [self.fetchedResultsController.sections count];
return numberOfSections;
}
// THIS IS NEVER BEING CALLED BECAUSE NUMBER OF SECTIONS IS RETURNED AS ZERO:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section];
NSInteger numberOfRows = [sectionInfo numberOfObjects];
return numberOfRows;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"default"];
Search *search = [self.fetchedResultsController objectAtIndexPath:indexPath];
cell.textLabel.text = search.query;
return cell;
}
我尝试过的:
- 我尝试将 FetchedResultsController 设置为 nil 并重新加载
table托管文档打开完成后的视图
(注释掉代码)。这并没有改变任何东西。
- 我检查了
UIManagedDocument
变量,它包含正确的值。
- 基础 SQL 数据库包含有效值。
我的问题
为什么我的代码 return 部分计数为零,您有什么想法或建议吗?
您需要在抓取结果控制器方法中调用 performFetch
。
我在 UITableViewController
中使用 NSFetchedResultsController
。
不幸的是,[self.fetchedResultsController.sections count]
return 的值为 0,这是不正确的。它应该 return 1 部分:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
NSInteger numberOfSections = [self.fetchedResultsController.sections count];
return numberOfSections;
}
这是我用来实例化 NSFetchedResultsController
并加载结果的代码:
@interface SearchQueryViewController ()
@property (strong, nonatomic) NSFetchedResultsController *fetchedResultsController;
@property Search *selectedSearch;
@end
@implementation SearchQueryViewController
- (void)viewWillAppear:(BOOL)animated
{
if (!self.managedDocument) {
[[MyDocumentHandler sharedDocumentHandler] performWithDocument:^(UIManagedDocument *document) {
self.managedDocument = document;
// Uncommenting the following code lines does not change the outcome:
/*self.fetchedResultsController = nil;
[self.tableView reloadData];*/
}];
}
[super viewWillAppear:animated];
}
# pragma Fetched Results Controller
- (NSFetchedResultsController *)fetchedResultsController
{
if (_fetchedResultsController) return _fetchedResultsController;
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Search"];
request.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"query" ascending:YES selector:@selector(localizedCaseInsensitiveCompare:)]];
self.fetchedResultsController = [[NSFetchedResultsController alloc]
initWithFetchRequest:request managedObjectContext:self.managedDocument.managedObjectContext
sectionNameKeyPath:nil cacheName:nil];
return _fetchedResultsController;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
NSInteger numberOfSections = [self.fetchedResultsController.sections count];
return numberOfSections;
}
// THIS IS NEVER BEING CALLED BECAUSE NUMBER OF SECTIONS IS RETURNED AS ZERO:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section];
NSInteger numberOfRows = [sectionInfo numberOfObjects];
return numberOfRows;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"default"];
Search *search = [self.fetchedResultsController objectAtIndexPath:indexPath];
cell.textLabel.text = search.query;
return cell;
}
我尝试过的:
- 我尝试将 FetchedResultsController 设置为 nil 并重新加载 table托管文档打开完成后的视图 (注释掉代码)。这并没有改变任何东西。
- 我检查了
UIManagedDocument
变量,它包含正确的值。 - 基础 SQL 数据库包含有效值。
我的问题
为什么我的代码 return 部分计数为零,您有什么想法或建议吗?
您需要在抓取结果控制器方法中调用 performFetch
。