I found an awesome tip at NSScreencast this week. Watch the video “Class Introspection” here.
Define an enum like so:
enum {
SomeViewControllerFeaturedTableViewSection = 0,
SomeViewControllerMainTableViewSection,
...
SomeViewControllerTableViewSectionCount
}
Ben from NSScreencast points out that the last value becomes a natural section count. So you can easily do:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return SomeViewControllerTableViewSectionCount;
}
And in the other delegate methods, something like this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.section == SomeViewControllerFeaturedTableViewSection) {
// make and configure a "featured" cell
return cell;
}
if (indexPath.section == SomeViewControllerMainTableViewSection) {
// make and configure a "main" cell
return cell;
}
...
}
Now you can simply move around the enum values to temporarily enable or disable sections:
enum {
SomeViewControllerFeaturedTableViewSection = 0,
SomeViewControllerTableViewSectionCount, // 1
SomeViewControllerMainTableViewSection, // 2 (because the count is 1, this will never appear)
}
I really like this trick and plan on using it often. Thanks again to NSScreencast for the idea.