2011-09-03 99 views
0

我遇到了從tableview中刪除單元格的問題。我正在使用從NSUserDefaults加載的NSMutableArray將單元加載到tableview中。 這是崩潰: 代碼:Tableview在刪除行時崩潰?

2011-09-03 18:21:06.097 App[10356:707] *** Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit/UIKit-1906/UITableView.m:1046 
2011-09-03 18:21:06.100 App[10356:707] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 0. 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 (3), plus or minus the number of rows inserted or deleted from that section (0 inserted, 1 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).' 

這是我如何刪除行... 代碼:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { 
    [self.cellArray objectAtIndex:indexPath.row]; 
    [tableView deleteRowsAtIndexPaths:[NSMutableArray arrayWithObject:indexPath] withRowAnimation:YES]; 
    [[NSUserDefaults standardUserDefaults] setObject:cellArray forKey:@"cellArray"]; 
} 

這是我numberOfRowsInSection委託方法: 代碼: //自定義表視圖中的行數。

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    self.cellArray = [NSMutableArray array]; 
    [self.cellArray addObjectsFromArray:[[NSUserDefaults standardUserDefaults] objectForKey:@"cellArray"]]; 

    if ([self.cellArray count] == 0) { 
     [ivstatstableView setHidden:YES]; 
     [sortbar setEnabled:NO]; 
    } 
    return [cellArray count]; 
} 

任何想法這裏有什麼問題嗎?

回答

1

您需要在從陣列中移除對象後調用reloadData。當您撥打reloadData方法時,您的新陣列會更新,您的新陣列可以讓您的UITableView根據新的更新數據加載新的單元格。