Friday, June 7, 2013

UITableView reloadSectionIndexTitles example in Objective C (iOS).


UITableView reloadSectionIndexTitles

Reloads the items in the index bar along the right side of the table view.

- (void)reloadSectionIndexTitles

Discussion of [UITableView reloadSectionIndexTitles]
This method gives you a way to update the section index after inserting or deleting sections without having to reload the whole table.

UITableView reloadSectionIndexTitles example.
[[self navigationItem] setRightBarButtonItem:nil];
[[self tableView] setScrollEnabled:YES];
[[self tableView] reloadData];
[[self tableView] flashScrollIndicators];
[[self tableView] sizeToFit];
[[self tableView] zoomScale];
[[self tableView] reloadSectionIndexTitles];

Example of [UITableView reloadSectionIndexTitles].
@implementation UITableView (JKAdditions)

- (UIView *)indexView {
    Class indexClass = NSClassFromString(@"UITableViewIndex");
    for(UIView *subview in self.subviews){
        if([subview isKindOfClass:indexClass]) return subview;
    }
    return nil;
}

- (void)reloadSectionIndexTitles {
    UIView *indexView = [self indexView];
    [indexView performSelector:@selector(setTitles:) withObject:[self.dataSource sectionIndexTitlesForTableView:self]];
    [indexView setNeedsDisplay];
}

@end

UITableView reloadSectionIndexTitles example.
- (void) setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated:animated];  // possibly not necessary depending upon your class hierarchy
    [tvController.tableView setEditing:editing animated:animated];
    [UIView animateWithDuration:0.25 delay:0.05 options:UIViewAnimationCurveLinear
                     animations:^{
                         [tvController.tableView beginUpdates];

                         if (editing == YES) {
                             [tvController.tableView deleteRowsAtIndexPaths:[settingsArray objectAtIndex:0] withRowAnimation:UITableViewRowAnimationFade];
                         } else {
                             UITableViewRowAnimation animation = animated ? UITableViewRowAnimationFade : UITableViewRowAnimationNone;
                             [tvController.tableView reloadRowsAtIndexPaths:[settingsArray objectAtIndex:0] withRowAnimation:animation];
                             [tvController.tableView reloadSectionIndexTitles];
                             self.navigationItem.hidesBackButton = editing;
                         }
                         [tvController.tableView endUpdates];
                     }
                     completion:nil];
}

End of UITableView reloadSectionIndexTitles example article.