0

我有一个带有一组地址的数组(数据源),我的表格视图设置为数据源中单元格数量的两倍,因此我可以将奇数单元格设置为小而清晰的样式(使表格视图看起来像单元格是分开的由一个小空间)。删除行时出现问题,我执行以下操作:

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
[tableView beginUpdates];
if (editingStyle==UITableViewCellEditingStyleDelete) {
        [self DeleteAddress:[ListAddress objectAtIndex:indexPath.row/2]];

        [ListAddress removeObjectAtIndex: indexPath.row/2];
        NSIndexPath *nextIndexPath = [[NSIndexPath alloc] initWithIndex:indexPath.row+1];

        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath,nextIndexPath,nil] withRowAnimation:UITableViewRowAnimationRight];
    }else if (editingStyle == UITableViewCellEditingStyleInsert){
         [self tableView:tableView didSelectRowAtIndexPath:indexPath];

    }
[tableView endUpdates];
}

DeleteAddress 方法从数据库中删除地址。当调试器到达 [tableview endUpdate] 函数时,会出现以下错误:

*** Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-2380.17/UITableView.m:1070
NSInternalInconsistencyException
4

2 回答 2

0

只需按照示例代码。从您的数据源中删除一行。希望它对您有用。此代码适用于我的应用程序。试试看

//  Swipe to delete has been used.  Remove the table item

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete)
    {
        //  Get a reference to the table item in our data array
        Pictures *itemToDelete = [self.pictureListData objectAtIndex:indexPath.row];

        //  Delete the item in Core Data
        [self.managedObjectContext deleteObject:itemToDelete];

        //  Remove the item from our array
        [pictureListData removeObjectAtIndex:indexPath.row];

        //  Commit the deletion in core data
        NSError *error;
        if (![self.managedObjectContext save:&error])
            NSLog(@"Failed to delete picture item with error: %@", [error domain]);

        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }   
}
于 2013-03-20T03:53:18.077 回答
0

我的问题是我以错误的方式创建了 nextIndexPath 变量。它应该是这样的:

NSIndexPath *nextIndexPath = [NSIndexPath indexPathForRow:indexPath.row+1 inSection:indexPath.section];

另外我不能同时删除两行,我需要从底部分别删除它们:

[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:nextIndexPath] withRowAnimation:UITableViewRowAnimationRight];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationRight];
于 2013-03-20T14:33:25.703 回答