2011-07-19 36 views
1

當我嘗試刪除單元格我得到崩潰......有問題,刪除單元格

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; 
} 
  • (無效)viewDidLoad中{ [super viewDidLoad];

    //取消註釋以下行以在此視圖控制器的 導航欄中顯示編輯按鈕。 self.navigationItem.rightBarButtonItem = self.editButtonItem;
    aContentArray = [[NSMutableArray arrayWithObjects:@「iPhone」,@「Android」,@「Blackberry」,@「Symbian」,nil] retain];
    aDetailTextArray = [[NSMutableArray arrayWithObjects:@「iPhone is from Apple Inc」,@「Android is from Google and it Open source」,@「Blackberry is from RIM Research In Motion」,@「Symbian is used 來自諾基亞「,無]保留];

}

// 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. 
    } 
} 

請幫我感謝

回答

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如何填充。以上只是一個特例的例子。

+0

請幫我解決.i已經添加了我的完整代碼。當我嘗試刪除單元格時,它仍然會崩潰應用程序 – user198725878

+0

在[tableView beginUpdates]調用之後放置removeObjectAtIndex調用。必須在beginUpdates和endUpdates之間對您的表和數據源進行所有更改。否則,你會看到你所看到的錯誤。 – sosborn