Uitableview 删除时tableview内部不一致
我有一个带有一组地址的数组(数据源),我的tableview设置为有两倍于数据源的单元格数量,因此我可以将奇数单元格设置为小而清晰(使tableview看起来像是单元格被一个小空间隔开)。删除行时出现问题,我执行以下操作:Uitableview 删除时tableview内部不一致,uitableview,ios6,Uitableview,Ios6,我有一个带有一组地址的数组(数据源),我的tableview设置为有两倍于数据源的单元格数量,因此我可以将奇数单元格设置为小而清晰(使tableview看起来像是单元格被一个小空间隔开)。删除行时出现问题,我执行以下操作: -(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)in
-(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
只需遵循示例代码。从数据源中删除一行。希望它对您有用。此代码适用于我的应用程序。请尝试
// 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];
}
}
我的问题是我以错误的方式创建了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];
你所犯的错误应该还有很多。最可能的问题是,代码从表中删除了两行(indexPath和nextIndexPath),但没有从数据源中删除两行,或者从数据源中删除了错误的行。此外,由于“插入”代码不起任何作用,因此您应该只在“删除”代码周围放置
beginUpdates
/endUpdates
调用。我的数据源没有奇数行的值,因为我创建它们只是为了美观,所以我只从数据源中删除行,但删除2个可视行。