2013-03-19 34 views
0

我有一组数据(数据源)与一组地址,我的tableview被设置为从数据源的单元格的数量的两倍,所以我可以风格的奇数单元格为小和清晰(使tableview看起来像单元格被一个小空格分隔)。删除一行时出现问题,我执行以下操作:tableview内部不一致时删除

-(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方法从数据库中删除地址。 当调试器到达[实现代码如下endUpdate]功能得到以下错误:

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

应该有更多的错误你正在得到。最可能的问题是您的代码从表中删除了两行(indexPath和nextIndexPath),但您不会从数据源中删除两行,或者从数据源中删除错误的行。另外,由于“插入”代码不执行任何操作,因此只应将'beginUpdates' /'endUpdates'调用放在“删除”代码的周围。 – rmaddy 2013-03-20 00:09:02

+0

我的数据源没有奇数行的值,因为我创建它们只是出于美学目的,所以我只删除数据源中的行,但删除2个可视行。 – jonathanwiesel 2013-03-20 14:35:20

回答

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]; 
0

只需按照样本code.Delete一行从数据source.Hope其有用you.This代码适用于我的应用程序。试试吧

// 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]; 
    } 
}