在上連接到NSFetchedResultsController一個的tableView的CELL(不是部分)刪除操作的響應我獲得這個錯誤:NSFetchedResultsControllerDelegate和deleteRowsAtIndexPaths:部分問題
「無效更新:的 部分無效數。包含在表視圖 更新後段 的數目(1)必須等於包含在表部分的數量 更新(2)之前 視圖,加上或 減去區間的數目插入 或刪除(0插入,0刪除)。'
我知道問題與更新之前和之前的部分編號有關。它說我沒有刪除部分,但後值與前值不同。
好吧,這是真的!但我的部分依賴於行單元格,因此如果我刪除某個部分的最後一行單元格,則該部分消失。
在這裏,我如何定義部分和行號: 創建部分以按屬性「日期」分組行。因此,如果一行具有屬性「日期」2010年4月10日和第二行有2010年4月11日,我有2節包含1行。
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return [[self.controller sections]count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
id <NSFetchedResultsSectionInfo> sectionInfo = [[self.controller sections] objectAtIndex:section];
return [sectionInfo numberOfObjects];
}
這裏我commitEditingStyle,在那裏我從表中刪除單元格,並從數據庫中刪除數據的定義(對象類型「交易」是的NSManagedObjectContext的子類,定義了我的模型)。
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
Transactions *trans = (Transactions *)[self.controller objectAtIndexPath:indexPath];
//Delete transaction
[self.context deleteObject:trans];
NSError *error = nil;
[self.context save:&error];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationTop];
}
}