ios - 无法获取 tableView 中的单元格数量

标签 ios objective-c uitableview core-data nsfetchedresultscontroller

我有一个主 tableViewController,触摸一个按钮 self.navigation 将推送 addItem viewController,单击保存 self.navigation 并弹出添加 VC,然后我们回到主 tableViewController,我成功添加和保存,也获取,但是当涉及到获取单元格数量时,数字返回0,这是方法

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

id <NSFetchedResultsSectionInfo> secInfo = [[self.fetchedResultsController sections]     objectAtIndex:section];
NSLog(@"No. of cells determined: %lu", (unsigned long)[secInfo numberOfObjects]);
return[secInfo numberOfObjects];

}

NSLog 给我 0,这是什么问题,这让我很头疼。

这是获取请求:

- (NSFetchedResultsController*) fetchedResultsController {

// Fetch Request
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Goal"
                                          inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"title"
                                                               ascending:NO];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[fetchRequest setSortDescriptors:sortDescriptors];




// setting _fethcedResultsController
_fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
                                                                managedObjectContext:self.managedObjectContext
                                                                  sectionNameKeyPath:nil
                                                                           cacheName:nil];




// setting _fetchedResultsController to self
_fetchedResultsController.delegate = self; // for the tableview updating thing



// Thats it
return _fetchedResultsController;

}

请注意,当我检查项目时,它不是零:

// fetching all items to check how many of them exists
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
[fetchRequest setEntity: [NSEntityDescription entityForName:@"Goal"
                                     inManagedObjectContext:self.managedObjectContext]];
NSManagedObjectContext *moc = self.managedObjectContext;

NSError *fetchError = nil;
NSArray *goals = [moc executeFetchRequest:fetchRequest error:&fetchError];

NSLog(@"No. of goals: %lu", (unsigned long)[goals count]);
// end of check for items

最佳答案

我忘了放这行代码:

if (_fetchedResultsController != nil) {
    return _fetchedResultsController;
 }

在方法中

- (NSFetchedResultsController*) fetchedResultsController; 

所以在我这样做之前,应用程序总是创建新的 fetchedResultsController 并覆盖旧的。

关于ios - 无法获取 tableView 中的单元格数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18779342/

相关文章:

ios - 在 UITableView 中仅重新加载一个部分标题

ios - 如何在我的 prepareForSegue 方法中获取 cell.textlabel.text 的值?

ios - UITableViewCell 背景隐藏 UI 元素

php - 如何使用 php 中的 session 变量将我的数据库添加到 html 表中

ios - audiokit:同时播放两个振荡器

ios - 将图像添加到 UIAlertController

ios - 如果IAP验证失败,该怎么办?

ios - 如何在 iphone 上使用 xmpp 框架将图像从一台设备传输到另一台设备?

ios - sqlite3_step(statement) == SQLITE_DONE 总是假的

iphone - 循环遍历字典;为什么我不能按字母顺序获取 key ?