Note that there are some explanatory texts on larger screens.

plurals
  1. POPopulating a sectioned UITableView
    text
    copied!<p>Ok, this question has probably been asked a couple of times but I can't quite find an example that helps me getting forward.</p> <p>I'm trying to create a sectioned UITableView that acts as some sort of history: it should be populated by an NSMutableArray that consists of HistoryBatchVO objects. Those HistoryBatchVO objects contain a timestamp (NSDate) and a names array (NSMutableArray) which in turn contains objects of type NameVO that contains (among other) a NSString.</p> <p>I'd like to use the timestamp as the sections header and the strings from NameVOs to be filled into the sections in the table accordingly.</p> <p>In my table controller I have:</p> <pre><code>-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return [_dataModel.history count]; } -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return 1; } </code></pre> <p>(Note: _dataModel.history is my NSMutableArray of HistoryBatchVOs). ... but I suppose <strong>numberOfRowsInSection</strong> needs to return the number of objects in my HistoryBatchVO.names array. Question is how do I do that?</p> <p>Also, how do I change the implementation for <strong>cellForRowAtIndexPath</strong> to get this working?</p> <pre><code>-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *cellID = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID forIndexPath:indexPath]; HistoryBatchVO *batchVO = [_dataModel.history objectAtIndex:indexPath.row]; return cell; } </code></pre> <hr> <p><strong>UPDATE: After solving the issue, Here's the working code:</strong></p> <pre><code>-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return [_dataModel.history count]; } -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { HistoryBatchVO *h = [_dataModel.history objectAtIndex:section]; return [h.names count]; } -(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { HistoryBatchVO *h = [_dataModel.history objectAtIndex:section]; return [NSString stringWithFormat:@"%@", h.timestamp]; } -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *cellID = @"Cell"; UITableViewCell *cell = nil; cell = [tableView dequeueReusableCellWithIdentifier:cellID forIndexPath:indexPath]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID]; } HistoryBatchVO *batchVO = [_dataModel.history objectAtIndex:indexPath.section]; NameVO *nameVO = [batchVO.names objectAtIndex:indexPath.row]; cell.textLabel.text = nameVO.string; return cell; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload