Skip to content

Instantly share code, notes, and snippets.

@delebedev
Created June 9, 2012 20:34
Show Gist options
  • Save delebedev/2902489 to your computer and use it in GitHub Desktop.
Save delebedev/2902489 to your computer and use it in GitHub Desktop.
Handy table view cells dequeuing + custom NIB support
//
// DLBaseTableViewCell.h
// Kawiky
//
// Created by Denis Lebedev on 2/24/12.
// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//
@interface DLBaseTableViewCell : UITableViewCell
+ (id)cellForTableView:(UITableView *)tableView;
+ (id)cellForTableView:(UITableView *)tableView fromNib:(UINib *)nib;
+ (NSString *)cellIdentifier;
+ (NSString *)nibName;
+ (UINib *)nib;
- (id)initWithCellIdentifier:(NSString *)cellID;
@end
//
// DLBaseTableViewCell.m
// Kawiky
//
// Created by Denis Lebedev on 2/24/12.
// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//
#import "DLBaseTableViewCell.h"
@implementation DLBaseTableViewCell
+ (id)cellForTableView:(UITableView *)tableView {
NSString *cellID = [self cellIdentifier];
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(([nibObjects count] > 0) &&
[[nibObjects objectAtIndex:0] isKindOfClass:[self class]],
@"Nib '%@' does not appear to contain a valid %@",
[self nibName], NSStringFromClass([self class]));
cell = [nibObjects objectAtIndex:0];
}
return cell;
}
+ (NSString *)cellIdentifier {
return NSStringFromClass([self class]);
}
+ (UINib *)nib {
NSBundle *classBundle = [NSBundle bundleForClass:[self class]];
return [UINib nibWithNibName:[self nibName] bundle:classBundle];
}
+ (NSString *)nibName {
return [self cellIdentifier];
}
- (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