【问题标题】:Having problem in deleting the cell删除单元格时遇到问题
【发布时间】:2011-07-19 03:27:32
【问题描述】:

当我尝试删除单元格时,我崩溃了......

Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-1447.6.4/UITableView.m:976
2011-07-19 08:55:53.575 MyTable[477:207] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 1.  The number of rows contained in an existing section after the update (3) must be equal to the number of rows contained in that section before the update (4), plus or minus the number of rows inserted or deleted from that section (0 inserted, 0 deleted).'

下面是我的代码

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
    }

    // Configure the cell...
    cell.textLabel.text = [aContentArray objectAtIndex:indexPath.row];
    cell.detailTextLabel.text =[aDetailTextArray objectAtIndex:indexPath.row];  
    cell.imageView.image = [UIImage imageNamed:@"home-picture.jpg"];

    return cell;
}
  • (void)viewDidLoad { [超级viewDidLoad];

    // 取消注释以下行以在 此视图控制器的导航栏。 self.navigationItem.rightBarButtonItem = self.editButtonItem;
    aContentArray = [[NSMutableArray arrayWithObjects:@"iPhone",@"Android",@"Blackberry",@"Symbian",nil]retain];
    aDetailTextArray = [[NSMutableArray arrayWithObjects:@"iPhone 是 来自 Apple Inc”,@“Android 来自 Google,它是开放的 source",@"Blackberry 来自 RIM Research In Motion",@"使用 Symbian 来自诺基亚",nil]retain];

}

// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {

    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source.
        [self.aDetailTextArray removeObjectAtIndex:indexPath.row];
        [self.aContentArray removeObjectAtIndex:indexPath.row];

        [tableView beginUpdates];

        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];

        [tableView endUpdates];

        [tableView reloadData];

    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
    }   
}

请帮帮我,谢谢

【问题讨论】:

    标签: iphone ios4


    【解决方案1】:

    问题是您删除了该行,但没有从数据源中删除该数据(再次阅读错误消息,您会明白我的意思)。如果不了解您的数据源是如何实现的,很难给出准确的答案,但作为演示,假设您将 tableview 内容保存在名为 items 的 NSMutableArray 中。

    [tableView beginUpdates];
    
    [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
    [items removeObjectAtIndex:indexpath.row]:
    [tableView endUpdates];
    //no need to call reloaddata
    

    同样,确切的方法将取决于您的表格视图的填充方式。以上只是一种特殊情况的示例。

    【讨论】:

    • 请帮帮我。我已经添加了我的完整代码。当我尝试删除单元格时,它仍然会使应用程序崩溃
    • 将 removeObjectAtIndex 调用放在 [tableView beginUpdates] 调用之后。对表和数据源的所有更改都必须发生在 beginUpdates 和 endUpdates 之间。否则你会得到你所看到的错误。
    猜你喜欢
    • 1970-01-01
    • 2021-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多