Это мой код, чтобы удалить ячейку в моем UITableView: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
}
}
Да, да! Вы были совершенно правы. Я думаю, что все, что мне было нужно, это кто-то, чтобы сказать мне, что моя проблема * не была * с моим кодом удаления. Я понял, что это связано с тем, как я всегда показывал один дополнительный раздел с ячейкой «Удалить все», и я не убирал его, когда последний раздел был удален. Короче говоря, вы помогли много, так что спасибо! –
@ Rickay Рад быть помощником. – MadhavanRP