Rafi
Rafi

Reputation: 1922

iOS - UITableView Delete All Rows Within A Section

I have a UITableView with expandable sections. When a user goes to another view, I need all the expanded sections to collapse, which I'll need to put in the viewWillDisappear method.

I've found solutions only on how to delete all rows from a table view at once, but is there a way to delete all the rows from a specific section?

EDIT:

I have figured out a solution, but I'm not sure if it's optimal or can lead to inefficiencies in the future. Whenever a cell is expanded, it gets added to an NSMutableIndexSet. So in my viewWillDisappear method, I iterate over the expanded sections like so:

-(void)viewWillDisappear:(BOOL)animated
{
    if (expandedSections.count != 0) {
        NSLog(@"COLLAPSING CALLED");
        [self.tableView beginUpdates];

        NSUInteger section = [expandedSections firstIndex];

        do
        {
            NSInteger rows;
            NSMutableArray *tmpArray = [NSMutableArray array];
            rows = [self tableView:self.tableView numberOfRowsInSection:section];
            [expandedSections removeIndex:section];
            for (int i=1; i<rows; i++) {
                NSIndexPath *tmpIndexPath = [NSIndexPath indexPathForRow:i inSection:section];
                [tmpArray addObject:tmpIndexPath];
            }
            [self.tableView deleteRowsAtIndexPaths:tmpArray withRowAnimation:UITableViewRowAnimationTop];
            NSIndexPath *expandableCellIndexPath = [NSIndexPath indexPathForRow:0 inSection:section];
            UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:expandableCellIndexPath];
            cell.accessoryView = [DTCustomColoredAccessory accessoryWithColor:[self.colorHolder objectAtIndex:section] type:DTCustomColoredAccessoryTypeRight];


            section = [expandedSections indexGreaterThanIndex:section];
        } while (section != NSNotFound);

        [self.tableView endUpdates];
    }
}

Please let me know if this is a good solution or, if I'm suspecting correctly, if this will lead to slower transitions between views in the future when there are more rows in each expanded section. Any help or advice would be appreciated. Thanks.

Upvotes: 0

Views: 991

Answers (2)

I did not read this in detail but surely the for loop should start at zero.

    for (int i=0; i<rows; i++) {
        NSIndexPath *tmpIndexPath = [NSIndexPath indexPathForRow:i inSection:section];
        [tmpArray addObject:tmpIndexPath];
    }

otherwise you will only delete all but the first cells in the section.

Upvotes: 0

Leonid Usov
Leonid Usov

Reputation: 1598

If you want to animate changes, you will need to first update your data source (to return 0 for number of rows in the section) then remove section and add section at the same index path in one transaction between [tv beginUpdates] [tv endUpdates]

Otherwise just update the data source and reload the table on your way back to the VC (if you don't want any animations)

Upvotes: 1

Related Questions