删除单元格时遇到问题

时间:2011-07-19 03:27:32

标签: iphone ios4

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

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 {   [super viewDidLoad];

         

    //取消注释以下行以显示“编辑”按钮   此视图控制器的导航栏。   self.navigationItem.rightBarButtonItem = self.editButtonItem;
      aContentArray = [[NSMutableArray   arrayWithObjects:@ “iPhone”,@ “机器人”,@ “黑莓”,@ “塞班”,零]保留];
        aDetailTextArray = [[NSMutableArray arrayWithObjects:@“iPhone是   来自Apple Inc“,@”Android来自谷歌,它是开放的   来源“,@”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.
    }   
}

请帮帮我,谢谢

1 个答案:

答案 0 :(得分:0)

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

[tableView beginUpdates];

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

同样,确切的方法将取决于您的tableview的填充方式。以上只是一个特例的例子。