Как удалить последнюю строку раздела?

Протестировано с Swift 2 решение @gabbler, если вы используете

self.navigationController?.navigationBar.hidden = true

Swift 3.0

self.navigationController?.navigationBar.isHidden = true

вместо

self.navigationController?.navigationBarHidden = true

, проведите назад жест работает как очарование!

15
задан Peter Hosey 12 October 2009 в 04:45
поделиться

2 ответа

Когда вы удаляете строку, и эта строка является последней в своем разделе, вам также необходимо удалить этот раздел. По сути, вам необходимо отслеживать как пути indexPath, которые вы хотите удалить, которые связаны со строками, так и индексы, относящиеся к разделам, которые необходимо удалить, поскольку они больше не содержат строк. Вы можете сделать это следующим образом:

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];

Это сработало для меня и других людей, которым я предложил подход.

27
ответ дан 1 December 2019 в 02:10
поделиться

Возможно, это сработает для вас:

- (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];
        }
    }
}

Надеюсь, это то, что вы искали. 1130826]

1
ответ дан 1 December 2019 в 02:10
поделиться
Другие вопросы по тегам:

Похожие вопросы: