Trong Swype xóa (hầu hết các dòng importatnt của phương pháp này):Xóa hàng theo quan điểm của bảng với fetchedResultController
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete)
{
Table *deleteRow = [self.fetchedResultsController objectAtIndexPath:indexPath];
[self.managedObjectContext deleteObject:deleteRow];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
}
Khi xóa hàng tôi nhận được lỗi này:
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 2.
The number of rows contained in an existing section after the update (1) must be equal to the number of rows contained in that section before the update (1), plus or minus the number of rows inserted or deleted from that section (0 inserted, 1 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).'
Nếu tôi nhận xét dòng cuối cùng của mã ([tableView deleteRowsAtIndexPaths:...]
) mọi thứ hoạt động tốt (nhưng tôi phải làm mới xem để xem hàng đó đã bị xóa).
Làm thế nào để làm điều đó đúng cách ..?
EDIT: Xét @Kyr Dunenkoff phản ứng tôi đã thêm:
- (void)controller:(NSFetchedResultsController*)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath*)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath*)newIndexPath
{
UITableView *tableV = [self tableView];
switch(type) {
case NSFetchedResultsChangeDelete:
[tableV deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
}
}
- (void)controllerDidChangeContent:(NSFetchedResultsController*)controller
{
[[self tableView] endUpdates];
}
- (void)controllerWillChangeContent:(NSFetchedResultsController*)controller
{
[[self tableView] beginUpdates];
}
Tuy nhiên điều này không thay đổi đâm & lỗi. Atm nó chỉ gây ra rằng thêm hàng mới không làm việc nữa.
Vui lòng kiểm tra cập nhật trên của tôi, sửa lỗi không hoạt động như tôi đã triển khai (có thể sai ..?). Cảm ơn bạn đã phản hồi! – Vive
Hm. Có gì trong phương thức [tableView: numberOfRowsInSection:] của bạn? Nó sẽ có một cái gì đó giống như 'return [[self.fetchedResultsController fetchedObjects] count];'. –