Friday, June 7, 2013

UITableView insertSections withRowAnimation example in Objective C (iOS).


UITableView insertSections withRowAnimation

Inserts one or more sections in the receiver, with an option to animate the insertion.

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

Parameters of [UITableView insertSections withRowAnimation]
sections
An index set that specifies the sections to insert in the receiving table view. If a section already exists at the specified index location, it is moved down one index location.
animation
A constant that indicates how the insertion is to be animated, for example, fade in or slide in from the left. See “Table Cell Insertion and Deletion Animation” for descriptions of these constants.

Discussion of [UITableView insertSections withRowAnimation]
UITableView calls the relevant delegate and data source methods immediately afterwards to get the cells and other content for visible cells.

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 insertSections withRowAnimation example.
-(void)sliderValueChanged:(id)slider {
   slide = slider.on;

   [tableView beginUpdates];

   if (slider.on) {
      [tableView insertSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationTop];
      // TODO: update data model by inserting new section
   } else {
      [tableView deleteSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationTop];
      // TODO: update data model by removing approprite section
   }

   [tableView endUpdates];
}

Example of [UITableView insertSections withRowAnimation].
if (segmentControl.selectedSegmentIndex == 0)
{
    self.settings.useMetric = YES;
    if ([sections containsObject:FT_AND_IN] && [sections containsObject:FRACTION_PRECISION]) {

        NSMutableIndexSet *indexSections = [NSMutableIndexSet indexSetWithIndex:[sections indexOfObject:FT_AND_IN]];
        [indexSections addIndex:[sections indexOfObject:FRACTION_PRECISION]];

        [sections removeObject:FT_AND_IN];
        [sections removeObject:FRACTION_PRECISION];

        [self.tableView deleteSections:indexSections
             withRowAnimation:UITableViewRowAnimationRight];
     }
 }
 else {
     self.settings.useMetric = NO;
     [sections insertObject:FT_AND_IN atIndex:1];
     [sections insertObject:FRACTION_PRECISION atIndex:2];

     NSMutableIndexSet *indexSections = [NSMutableIndexSet indexSetWithIndex:[sections indexOfObject:FT_AND_IN]];
     [indexSections addIndex:[sections indexOfObject:FRACTION_PRECISION]];

     [self.tableView insertSections:indexSections
          withRowAnimation:UITableViewRowAnimationRight];
}

UITableView insertSections withRowAnimation example.

[self.tableView beginUpdates];

for (i = 0 i < changeCount; i++) {
  type = changes[i];

  switch(type) {
    case NSFetchedResultsChangeInsert:
      [self.tableView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex]
                    withRowAnimation:UITableViewRowAnimationFade];
      break;

    case NSFetchedResultsChangeDelete:
      [self.tableView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex]
                    withRowAnimation:UITableViewRowAnimationFade];
      break;
  }
}

switch(type) {

  case NSFetchedResultsChangeInsert:
    [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath]
                          withRowAnimation:UITableViewRowAnimationFade];
    break;

  case NSFetchedResultsChangeDelete:
    [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
                          withRowAnimation:UITableViewRowAnimationFade];
    break;

  case NSFetchedResultsChangeUpdate:
    [self configureCell:(id)[tableView cellForRowAtIndexPath:indexPath]
            atIndexPath:indexPath];
    break;

  case NSFetchedResultsChangeMove:
    [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
                          withRowAnimation:UITableViewRowAnimationFade];
    [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:newIndexPath.section]
                  withRowAnimation:UITableViewRowAnimationFade];
    break;
  }
}

[self.tableView endUpdates];
share|improve this answer

End of UITableView insertSections withRowAnimation example article.