Guest User

Untitled

a guest
Feb 20th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.70 KB | None | 0 0
  1. #import "ViewFactory.h"
  2.  
  3. @implementation ViewFactory
  4.  
  5. - (id) initWithNib:(NSString*)aNibName
  6. {
  7. if (self == [super init]) {
  8. viewTemplateStore = [[NSMutableDictionary alloc] init];
  9. NSArray * templates = [[NSBundle mainBundle] loadNibNamed:aNibName owner:self options:nil];
  10. for (id template in templates) {
  11. if ([template isKindOfClass:[UITableViewCell class]]) {
  12. UITableViewCell * cellTemplate = (UITableViewCell *)template;
  13. NSString * key = cellTemplate.reuseIdentifier;
  14. if (key) {
  15. [viewTemplateStore setObject:[NSKeyedArchiver
  16. archivedDataWithRootObject:template]
  17. forKey:key];
  18. } else {
  19. @throw [NSException exceptionWithName:@"Unknown cell"
  20. reason:@"Cell has no reuseIdentifier"
  21. userInfo:nil];
  22. }
  23. }
  24. }
  25. }
  26.  
  27. return self;
  28. }
  29.  
  30. - (void) dealloc
  31. {
  32. [viewTemplateStore release];
  33. [super dealloc];
  34. }
  35.  
  36. - (UITableViewCell*)cellOfKind:(NSString*)theCellKind forTable:(UITableView*)aTableView
  37. {
  38. UITableViewCell *cell = [aTableView dequeueReusableCellWithIdentifier:theCellKind];
  39.  
  40. if (!cell) {
  41. NSData * cellData = [viewTemplateStore objectForKey:theCellKind];
  42. if (cellData) {
  43. cell = [NSKeyedUnarchiver unarchiveObjectWithData:cellData];
  44. } else {
  45. NSLog(@"Don't know nothing about cell of kind %@", theCellKind);
  46. }
  47. }
  48.  
  49. return cell;
  50. }
  51.  
  52. @end
Add Comment
Please, Sign In to add comment