Это мой код для удаления ячейки в моем UITableView:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView beginUpdates];
if([tableView numberOfRowsInSection:[indexPath section]] > 1) {
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
withRowAnimation:UITableViewRowAnimationFade];
} else {
[tableView deleteSections:[NSIndexSet indexSetWithIndex:[indexPath section]]
withRowAnimation:UITableViewRowAnimationFade];
}
[tableView endUpdates];
}
Перед удалением ячейки также обновляются некоторые данные, но я уверен, что они обновляются правильно, потому что count
массива источника данных всегда на единицу меньше при каждом удалении ячейки. Это ожидаемое поведение. Однако по какой-то причине всякий раз, когда я удаляю последнюю ячейку в разделе, я получаю 'Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (0) must be equal to the number of rows contained in that section before the update (1), plus or minus the number of rows inserted or deleted from that section (0 inserted, 0 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).'
Это невероятно расстраивает, особенно потому, что, судя по тому, что мне удалось найти в Интернете, я делаю это правильно. Может мне нужно поспать, это было давно. Есть идеи здесь?
Решение:
- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section {
if(tableIsEmpty) {
//**this** line was the culprit- returning zero here fixed the problem
return 0;
} else if(section == ([tableView numberOfSections] - 1)) {
//this is the last section- it only needs one row for the 'Delete all' button
return 1;
} else {
//figure out how many rows are needed for the section
}
}