Skip to content

Instantly share code, notes, and snippets.

@langford
Created August 10, 2014 17:31
Show Gist options
  • Save langford/c0d0cec92017b44fc72c to your computer and use it in GitHub Desktop.
Save langford/c0d0cec92017b44fc72c to your computer and use it in GitHub Desktop.
Instead of this in cellForRowAtIndexpath
In init:
-(instancetype)init{
...
_sections = @[
@[@"Twitter",@"Blog",@"Contact Us"],
@[@"nameone",@"nametwo",@"namethree"]
];
...
}
- (NSInteger)numberOfSectionsInTableView:(UITableView*)tableView{
return self.sections.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [self.sections[section] count];
}
- (UITableViewCell*)tableView:(UITableView*)tableview cellForRowAtIndexPath:(NSIndexPath*){
...
cell.textLabel.text = sections[indexPath.section][indexPath.row];
---
}
//
//instead of
/*
if (indexPath.section == 0) {
if (indexPath.row == 0) {
cell.textLabel.text = @"Twitter"
} else if (indexPath.row == 1) {
cell.textLabel.text = @"Blog"
} else {
cell.textLabel.text = @"Contact Us"
}
} else {
if (indexPath.row == 0) {
cell.textLabel.text = @"nameone"
} else if (indexPath.row == 1) {
cell.textLabel.text = @"nametwo"
} else {
cell.textLabel.text = @"namethree"
}
}
*/
Benefits: No ifs/switches/etc except in delegate method, which can switch off the string. No Runtime crashes when one is deleted or moved to a new section
I wouldn't have said anything if it wasn't put forth as "Doing a familiar and commonplace task" and will be found by beginners.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment