我正在使用NSFetchedResultsController。以前我有一個類似的問題,當數據庫沒有用於tableview的條目但創建了一個時,我發現必須至少有一個部分,所以我解決了這個問題。但現在它崩潰了,例如我有兩個部分,每個部分都有一行,我刪除了一行,所以部分應該消失 - >崩潰。它表示更新前的部分數量(2)不等於刪除的數量(0)。使用NSFetchedResultsController刪除節中的最後一行 - >崩潰,使用NSFetchedResultsController
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return 1 if the fetchedResultsController section count is zero
return [[fetchedResultsController sections] count] ? : 1;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
// check if we really have any sections in the managed object:
if (!fetchedResultsController.sections.count) return @"Persoonlijk";
id <NSFetchedResultsSectionInfo> sectionInfo = [[fetchedResultsController sections] objectAtIndex:section];
return [sectionInfo name];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// check if we really have any sections in the managed object:
if (!fetchedResultsController.sections.count) return 0;
id <NSFetchedResultsSectionInfo> sectionInfo = [[fetchedResultsController sections] objectAtIndex:section];
return [sectionInfo numberOfObjects];
}
更新 方法,其中行被刪除:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete schedule
NSManagedObjectContext *context = [fetchedResultsController managedObjectContext];
[context deleteObject:[fetchedResultsController objectAtIndexPath:indexPath]];
// Save the context.
NSError *error = nil;
if (![context save:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
exit(-1);
}
}
}
這是怎麼回事:return [[fetchedResultsController sections] count]? :1;你錯過了中間的參數。我想你實際上錯過了第一個,並讓他們處於錯誤的位置。 – Jim 2012-03-06 21:50:44
它看起來很奇怪,但實際上是正確的:)這是一個簡短的手返回版本[[fetchedResultsController部分] count]> 0? [[fetchedResultsController sections] count]:1; – Pieter 2012-03-06 21:58:19
我發現這條消息通常意味着在實際獲得消息進行刪除/刪除的方法中出現了問題。從你的文章看來,你實際上並沒有改變它的部分數量?也許發佈您的部分刪除代碼? – AlexTheMighty 2012-03-06 22:06:05