Posted By

bethjt1220 on 02/23/11


Tagged

ios


Versions (?)

IOS TableViews in Navigation Controller


 / Published in: iPhone
 

Pulls data from a plist set up as a dictionary of arrays of strings. Creates multi-level TableView that can be nested inside a navigation controller. Snippet shows key methods for 2 nested levels. 2 levels --> 2 classes (both of which extend UITableViewController)

  1. // ---------- Top Level ----------------
  2. - (void)viewDidLoad {
  3.  
  4. self.title = @"Categories";
  5. NSMutableArray *array = [[NSMutableArray alloc] init];
  6.  
  7. NSString *path = [[NSBundle mainBundle] pathForResource:@"CategoryData"
  8. ofType:@"plist"];
  9. NSDictionary *dict = [[NSDictionary alloc]
  10. initWithContentsOfFile:path];
  11. self.buildings = dict;
  12. [dict release];
  13.  
  14. for(id name in buildings) {
  15. CategorySecondLevelViewController *controller = [[CategorySecondLevelViewController alloc]
  16. initWithStyle:UITableViewStylePlain];
  17. controller.title = name;
  18.  
  19. [array addObject:controller];
  20. [controller release];
  21. }
  22.  
  23. self.categories = array;
  24. [array release];
  25.  
  26. [super viewDidLoad];
  27. }
  28.  
  29. - (UITableViewCell *)tableView:(UITableView *)tableView
  30. cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  31.  
  32. static NSString *CategoryCell = @"CategoryCell";
  33.  
  34. // reuse cell if possible
  35. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:
  36. CategoryCell];
  37. if(cell == nil) {
  38. cell = [[[UITableViewCell alloc]
  39. initWithStyle: UITableViewCellStyleDefault
  40. reuseIdentifier: CategoryCell] autorelease];
  41. }
  42.  
  43. // configure cell
  44. NSUInteger row = [indexPath row];
  45.  
  46. // extension point to include images if wanted
  47. UITableViewController *controller = [categories objectAtIndex:row];
  48. cell.textLabel.text = controller.title;
  49. cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
  50. return cell;
  51. }
  52.  
  53. // ---------- Second Level ----------------
  54. - (void)viewDidLoad {
  55.  
  56. NSString *path = [[NSBundle mainBundle] pathForResource:@"CategoryData"
  57. ofType:@"plist"];
  58. NSDictionary *dict = [[NSDictionary alloc]
  59. initWithContentsOfFile:path];
  60.  
  61. NSString *key = self.title;
  62. NSArray *values = [dict objectForKey:key];
  63.  
  64. self.list = values;
  65. [dict release];
  66.  
  67. [super viewDidLoad];
  68. }
  69.  
  70. - (UITableViewCell *)tableView:(UITableView *)tableView
  71. cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  72.  
  73. static NSString *BuildingIdentifier = @"BuildingIdentifier";
  74. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:
  75. BuildingIdentifier];
  76. if(cell == nil) {
  77. cell = [[[UITableViewCell alloc]
  78. initWithStyle:UITableViewCellStyleDefault
  79. reuseIdentifier:BuildingIdentifier] autorelease];
  80. }
  81.  
  82. NSUInteger row = [indexPath row];
  83. cell.textLabel.text = [list objectAtIndex:row];
  84.  
  85. return cell;
  86. }

Report this snippet  

You need to login to post a comment.