code_junkie

Loading a Reusable UITableViewCell from a Nib

Nov 13th, 2011
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.03 KB | None | 0 0
  1. - (NSString *) reuseIdentifier {
  2. return @"myIdentifier";
  3. }
  4.  
  5. - (UITableViewCell *)tableView:(UITableView *)tableView
  6. cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  7.  
  8. static NSString *CellIdentifier = @"CustomTableCell";
  9. static NSString *CellNib = @"CustomTableCellView";
  10.  
  11. UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  12. if (cell == nil) {
  13. NSArray *nib = [[NSBundle mainBundle] loadNibNamed:CellNib owner:self options:nil];
  14. cell = (UITableViewCell *)[nib objectAtIndex:0];
  15. }
  16.  
  17. // perform additional custom work...
  18.  
  19. return cell;
  20. }
  21.  
  22. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  23. UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"CustomCellId"];
  24. if (cell == nil) {
  25. UIViewController *c = [[UIViewController alloc]
  26. initWithNibName:@"CustomCell" bundle:nil];
  27.  
  28. cell = (PostCell *)c.view;
  29. [c release];
  30. }
  31. return cell;
  32. }
  33.  
  34. static NSData *sLoadedCustomCell = nil;
  35.  
  36. cell = [tableView dequeueReusableCellWithIdentifier:@"CustomCell"];
  37. if (cell == nil)
  38. {
  39. if (sLoadedCustomCell == nil)
  40. {
  41. // Load the custom table cell xib
  42. // and extract a reference to the cell object returned
  43. // and cache it in a static to avoid reloading the nib again.
  44.  
  45. for (id loadedObject in [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:nil options:nil])
  46. {
  47. if ([loadedObject isKindOfClass:[UITableViewCell class]])
  48. {
  49. sLoadedCustomCell = [[NSKeyedArchiver archivedDataWithRootObject: loadedObject] retain];
  50. break;
  51. }
  52. }
  53. cell = (UITableViewCell *)[NSKeyedUnarchiver unarchiveObjectWithData: sLoadedCustomCell];
  54. }
  55.  
  56. ChainesController *c = [[ChainesController alloc] initWithNibName:@"ChainesController" bundle:nil];
  57. [[NSBundle mainBundle] loadNibNamed:@"ChaineArticleCell" owner:c options:nil];
  58. cell = [c.blogTableViewCell retain];
  59. [c release];
Add Comment
Please, Sign In to add comment