title: Objective C Dynamic NSDictionary for UI Table View divided by Section Headers
body: <p>So this is based off of my question here</p>
  
  <p><a href="https://stackoverflow.com/questions/14493725/objective-c-when-to-use-nsdictionary-instead-of-nsarray#comment20201177_14493725">objective c when to use NSDictionary instead of NSArray</a></p>
  
  <p>Apologies for the noob questions. I just started learning objective c and I'm really confused. </p>
  
  <pre><code>- (void)viewDidLoad
  {
      [super viewDidLoad];
      headers = [NSArray arrayWithObjects:@"Header Section 1 (H1)", @"Header Section 2 (H2)", nil];
      events = [NSArray arrayWithObjects:@"H1 Content 1", @"H1 Content 2", nil];
      myInfo = [NSArray arrayWithObjects:@"H2 Content 1", @"H2 Content 2", @"H2 Content 3", nil];
  
      menuDetails = [[NSMutableDictionary alloc] init];
      for(NSString *event in events){
          [menuDetails setValue:event forKey:@"Header Section 1 (H1)"];
      }
  
      for(NSString *info in myInfo){
          [menuDetails setValue:info forKey:@"Header Section 1 (H2)"];
      }
  }
  
  
  -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
      return [[menuDetails allKeys] count];
  }
  
  -(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
      return [[[menuDetails allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)]objectAtIndex:section];
  }
  </code></pre>
  
  <ol>
  <li><p>Wanted to limit this for the First Section but not on the 2nd section</p>
  
  <pre><code>-(NSInteger)tableView:(UITableView *)tableview numberOfRowsInSection:(NSInteger)section {
      return 5;
  }
  </code></pre></li>
  <li><p>So I'm not really sure how to make this dynamic when I try to fill up the cells with this method. I'm not really sure how to make sure it selects the "Key" of the section the specific cell is at (blocked line) :</p></li>
  </ol>
  
  <hr>
  
  <pre><code>-(UITableViewCell *)tableView:(UITableView *) tableView cellForRowAtIndexPath:(NSIndexPath *) indexPath {
      static NSString *CellIdentifier = @"OptionCellIdentifier";
  
          MenuOptionTableCell *cell = (MenuOptionTableCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  
          if(cell == nil){
              NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"OptionCellIdentifier" owner:self options:nil];
              cell = [nib objectAtIndex:0];
          }
  
          NSDictionary *menuItem = [[menuDetails valueForKey:[[[menuDetails allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)] objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row];
  
          cell.optionName.text = [menuItem objectForKey:@"Events"];
  
          return cell;
      }
  </code></pre>
  
  <p>EDIT: GAAAHHH, the formatting is killing me. It won't get inside the code markup</p>
  
  <p>This is where Idk what to do:</p>
  
  <blockquote>
    <p>cell.optionName.text = [menuItem objectForKey:@"Events"];</p>
  </blockquote>
  
  <p>So what I wanted to happen is:</p>
  
  <ul>
  <li>Section Header 1
  
  <ol>
  <li>H1 Content 1</li>
  <li>H1 Content 2</li>
  </ol></li>
  <li>Section Header 1
  
  <ol>
  <li>H2 Content 1</li>
  <li>H2 Content 2</li>
  <li>H1 Content 3</li>
  </ol></li>
  </ul>