Created
November 15, 2008 19:05
-
-
Save Pegolon/25299 to your computer and use it in GitHub Desktop.
ViewFactory for using UITableViewCell in Interface Builder
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "ViewFactory.h" | |
@implementation ViewFactory | |
- (id) initWithNib:(NSString*)aNibName | |
{ | |
if (self == [super init]) { | |
viewTemplateStore = [[NSMutableDictionary alloc] init]; | |
NSArray * templates = [[NSBundle mainBundle] loadNibNamed:aNibName owner:self options:nil]; | |
for (id template in templates) { | |
if ([template isKindOfClass:[UITableViewCell class]]) { | |
UITableViewCell * cellTemplate = (UITableViewCell *)template; | |
NSString * key = cellTemplate.reuseIdentifier; | |
if (key) { | |
[viewTemplateStore setObject:[NSKeyedArchiver | |
archivedDataWithRootObject:template] | |
forKey:key]; | |
} else { | |
@throw [NSException exceptionWithName:@"Unknown cell" | |
reason:@"Cell has no reuseIdentifier" | |
userInfo:nil]; | |
} | |
} | |
} | |
} | |
return self; | |
} | |
- (void) dealloc | |
{ | |
[viewTemplateStore release]; | |
[super dealloc]; | |
} | |
- (UITableViewCell*)cellOfKind:(NSString*)theCellKind forTable:(UITableView*)aTableView | |
{ | |
UITableViewCell *cell = [aTableView dequeueReusableCellWithIdentifier:theCellKind]; | |
if (!cell) { | |
NSData * cellData = [viewTemplateStore objectForKey:theCellKind]; | |
if (cellData) { | |
cell = [NSKeyedUnarchiver unarchiveObjectWithData:cellData]; | |
} else { | |
NSLog(@"Don't know nothing about cell of kind %@", theCellKind); | |
} | |
} | |
return cell; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment