Friday, June 7, 2013

UITableView deleteSections example in Objective C (iOS).


UITableView deleteSections

Deletes one or more sections in the receiver, with an option to animate the deletion.

- (void)deleteSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation

Parameters of [UITableView deleteSections]
sections
An index set that specifies the sections to delete from the receiving table view. If a section exists after the specified index location, it is moved up one index location.
animation
A constant that either specifies the kind of animation to perform when deleting the section or requests no animation. See “Table Cell Insertion and Deletion Animation” for descriptions of the constants.

Discussion of [UITableView deleteSections]
Note the behavior of this method when it is called in an animation block defined by the beginUpdates and endUpdates methods. UITableView defers any insertions of rows or sections until after it has handled the deletions of rows or sections. This happens regardless of ordering of the insertion and deletion method calls. This is unlike inserting or removing an item in a mutable array, where the operation can affect the array index used for the successive insertion or removal operation. For more on this subject, see “Batch Insertion and Deletion of Rows and Sections” in Table View Programming Guide for iOS.

UITableView deleteSections example.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete)
    {
        // modelForSection is a custom model object that holds items for this section.
        [modelForSection removeItem:[self itemForRowAtIndexPath:indexPath]];

        [tableView beginUpdates];

        // Either delete some rows within a section (leaving at least one) or the entire section.
        if ([modelForSection.items count] > 0)
        {
            // Section is not yet empty, so delete only the current row.
            [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
                             withRowAnimation:UITableViewRowAnimationFade];
        }
        else
        {
            // Section is now completely empty, so delete the entire section.
            [tableView deleteSections:[NSIndexSet indexSetWithIndex:indexPath.section]
                     withRowAnimation:UITableViewRowAnimationFade];
        }

        [tableView endUpdates];
    }
}

Example of [UITableView deleteSections].
[table beginUpdates];
[myArray removeObjectAtIndex:myIndexPath.section];
[myArrayAnswerCount removeObjectAtIndex:myIndexPath.section]; //this is what i forgot.
[table deleteSections:[NSIndexSet indexSetWithIndex:myIndexPath.section] withRowAnimation:YES];
[table endUpdates];

UITableView deleteSections example.

- (IBAction)deleteButtonPressed:(UIButton *)sender {
    NSInteger section = sender.tag - 1000;
    [self.objects removeObjectAtIndex:section];
    [self.tableView deleteSections:[NSIndexSet indexSetWithIndex:section] withRowAnimation:UITableViewRowAnimationAutomatic];

    // reload sections to get the new titles and tags
    NSInteger sectionCount = [self.objects count];
    NSIndexSet *indexes = [NSMutableIndexSet indexSetWithIndexesInRange:NSMakeRange(0, sectionCount)];
    [self.tableView reloadSections:indexes withRowAnimation:UITableViewRowAnimationNone];
}

End of UITableView deleteSections example article.