Skip to content

Instantly share code, notes, and snippets.

@kulte
Created December 8, 2012 20:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kulte/4241857 to your computer and use it in GitHub Desktop.
Save kulte/4241857 to your computer and use it in GitHub Desktop.
Smart UITableViewCell subclass to abstract away scattered cell boilerplate.
@interface KTEManagedTableViewCell : UITableViewCell { }
+ (id)cellForTableView:(UITableView *)tableView;
+ (id)cellForTableView:(UITableView *)tableView fromNib:(UINib *)nib;
+ (NSString *)cellIdentifier;
- (id)initWithCellIdentifer:(NSString *)cellID;
@end
@implementation KTEManagedTableViewCell
+ (id)cellForTableView:(UITableView *)tableView {
NSString *cellID = [self cellIdentifer];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
if (cell == nil) {
cell = [[self alloc] initWithCellIdentifier:cellID];
}
return cell;
}
+ (id)cellForTableView:(UITableView *)tableView fromNib:(UINib *)nib {
NSString *cellID = [self cellIdentifier];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
if (cell == nil) {
NSArray *nibObjects = [nib instantiateWithOwner:nil options:nil];
NSAssert2([...]);
cell = [nibObjects objectAtIndex:0];
}
return cell;
}
+ (NSString *)cellIdentifier {
return NSStringFromClass([self class]);
}
- (id)initWithCellIdentifier:(NSString *)cellID {
return [self initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellID];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment