這個問題讓我在最後幾個小時裏很忙。我有兩節,每節都有一行。當我刪除某一節中的行時,它會拋出一個異常,說這是一個無效更新(更新前後的行數/節數不相同)。這是可以理解的,因爲我刪除了一節的最後一行,因此我刪除了該節。問題是如何避免例外。如何刪除一節的最後一行?
一切都可以與我的數據源。我檢查並重新檢查(相信我)。
因此,作爲線程的標題狀態,如何刪除一節的最後一行而不會發生異常?
感謝,
巴特
這個問題讓我在最後幾個小時裏很忙。我有兩節,每節都有一行。當我刪除某一節中的行時,它會拋出一個異常,說這是一個無效更新(更新前後的行數/節數不相同)。這是可以理解的,因爲我刪除了一節的最後一行,因此我刪除了該節。問題是如何避免例外。如何刪除一節的最後一行?
一切都可以與我的數據源。我檢查並重新檢查(相信我)。
因此,作爲線程的標題狀態,如何刪除一節的最後一行而不會發生異常?
感謝,
巴特
當你刪除一個行,此行是其部分的最後一個,您還需要刪除該部分。基本上,您需要跟蹤要刪除的索引路徑(與行關聯)以及與不需要包含行的部分需要刪除的索引。可以按如下步驟去做:
NSMutableIndexSet *indexes = [NSMutableIndexSet indexSet];
每次從相關的tableView的特定部分模型陣列中刪除對象,檢查陣列計數爲零,在這種情況下,添加表示部分索引到索引:
[array removeObjectAtIndex:indexPath.row];
if(![array count])
[indexes addIndex: indexPath.section];
確定所有相關的行indexPaths的被刪除,然後更新的tableView如下:
[tableView beginUpdates];
[tableView deleteRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationFade];
[tableView deleteSections:indexes withRowAnimation:UITableViewRowAnimationFade];
[tableView endUpdates];
這爲我工作和鄰我建議這種方法的人。
這個選項可以正常工作,但是當你刪除最後一個部分時會拋出另一個異常(但我會想辦法解決這個問題)。我很難相信這是唯一的方法。它看起來很蘋果/不雅。它不只是工作;-)。儘管感謝您的意見。 – 2009-06-16 14:21:50
也許,這可能爲你工作:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
NSUInteger section = [indexPath section];
NSUInteger row = [indexPath row];
NSString *key = [self.keys objectAtIndex:section];
NSMutableArray *rowsInSection = [self.dict objectForKey:key];
[rowsInSection removeObjectAtIndex:row];
NSUInteger rowsInSectionCount = [rowsInSection count];
if (rowsInSectionCount > 0) {
// If we still have rows in this section just delete the row
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
else {
// There are no more rows in this section
[self.dict removeObjectForKey:key];
[self.keys removeObjectAtIndex:section];
NSUInteger sectionCount = [self.keys count];
if (sectionCount > 0) {
// If we still have 1 or more sections left just delete this section
[tableView deleteSections:[NSIndexSet indexSetWithIndex:section] withRowAnimation:UITableViewRowAnimationFade];
}
else {
// If there are no more rows and sections left just delete the last row
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[tableView reloadData];
}
}
}
希望這是你所期待的。
這一個應該做的伎倆:
// Either delete some rows within a section (leaving at least one) or the entire section.
if ([indexPath section] < 0) {
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
} else if ([indexPath section] == 0) {
[self.tableView deleteSections:[NSIndexSet indexSetWithIndex:[indexPath section]] withRowAnimation:UITableViewRowAnimationFade];
}
對於斯威夫特2,在你的模型中移除後的數據:
tableView.beginUpdates()
let indexSet = NSMutableIndexSet()
indexSet.addIndex(indexPath.section)
tableView.deleteSections(indexSet, withRowAnimation: UITableViewRowAnimation.Fade)
tableView.endUpdates()
「線程的標題是」這是一個問答網站,不是論壇。你的意思是問題的標題。 – 2009-10-12 04:44:37