TableView删除错误

时间:2011-04-19 20:31:44

标签: ios objective-c iphone xcode uitableview

我从以下代码中收到以下错误:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [self.tableView reloadData];
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
        [[FavouritesController sharedController] removeFavouriteAtIndex:indexPath.row];
        [self.tableView reloadData];
    }   
    else if (editingStyle == UITableViewCellEditingStyleNone) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}

请告诉我如何修理它?

  

***断言失败 - [UITableView _endCellAnimationsWithContext:],/ SourceCache / UIKit / UIKit-1448.89 / UITableView.m:995   2011-04-19 21:30:17.854 APPNAME [546:707] ***由于未捕获的异常'NSInternalInconsistencyException'而终止应用程序,原因:'无效更新:第0节中的行数无效。包含在更新后的现有部分(4)必须等于更新前的该部分中包含的行数(4),加上或减去从该部分插入或删除的行数(插入0,删除1)。 / p>

2 个答案:

答案 0 :(得分:0)

你可能想要这段代码:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [[FavouritesController sharedController] removeFavouriteAtIndex:indexPath.row];
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }   
    else if (editingStyle == UITableViewCellEditingStyleNone) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}

答案 1 :(得分:0)

在更换表格之前更改模型。

所以试试:

[[FavouritesController sharedController] removeFavouriteAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];

您可能不需要所有这些重新加载,除非模型更改的不仅仅是那个索引路径。行删除会自行重新加载。