2012-11-18 106 views
0

所以我有這樣的代碼在我的tableview刪除行:UITableView單元格刪除崩潰?

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

現在我做indexPath.row/2,因爲我這樣做:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    return [cellArray count] * 2; 
} 

正因爲它是什麼讓我的UI外觀事情是這樣的。無論如何這是崩潰:

***由於未捕獲異常'NSInternalInconsistencyException',原因:'無效的更新:無效的行數在第0節中的行數。包含在更新(42)必須等於在更新前(44)中包含在該部分中的行數,加上或減去從該部分插入或刪除的行數(0插入,1刪除)以及正數或負數行移入或移出該部分(0移入,0移出)。'

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { 
    NSIndexPath *nextPath = [NSIndexPath indexPathForRow:indexPath.row + 1 inSection:indexPath.section]; 
    [self.cellArray removeObjectAtIndex:indexPath.row/2]; 
    [[NSUserDefaults standardUserDefaults] setObject:self.cellArray forKey:@"cellArray"]; 
    [tableView deleteRowsAtIndexPaths:[NSMutableArray arrayWithObjects:indexPath, nextPath, nil] withRowAnimation:UITableViewRowAnimationFade]; 
} 

回答

1

問題是您的單元格數組中的每個對象使用兩個錶行。但是在你發佈的代碼中,你只能從表中刪除一行。您需要刪除兩行。

NSIndexPath *nextPath = [NSIndexPath indexPathForRow:indexPath.row + 1 inSection:indexPath.section]; 
[tableView deleteRowsAtIndexPaths:[NSMutableArray arrayWithObjects:indexPath, nextPath, nil] withRowAnimation: UITableViewRowAnimationFade]; 

順便說一句 - YES不是行動畫的有效參數。

+0

檢查我發佈的更新代碼,是你說我應該做什麼呢? –

+3

是的。但不要修改你原來的問題。現在沒有人知道什麼是錯的。您應該放回代碼,然後在問題末尾添加更新後的代碼,以便人們可以查看歷史記錄。 – rmaddy

+0

好吧,我回滾了,感謝您的幫助! –

1

也許它只是在這條線的小錯字。

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    [self.cellArray removeObjectAtIndex:indexPath.row]; // Returning a number instead of possible double 
    ... 
}