Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Darren's answer gave me the idea for what worked for me, however I didn't have to go so far as to implement every single tableView delegate method. You really only need to override numberOfRowsInSection and cellForRowAtIndexPath.</p> <p>First I defined a static table in Interface Builder with 4 sections, 2 to 4 cells per section. I wanted section 0, 2 and 3 to be static and look exactly as they did in IB, but I wanted section 1 to have a custom number of rows with a custom display in each cell based on an array of values I had.</p> <p>In the view controller for the static table, override the number of cells returned for your dynamic section, but accept the defaults for all other sections (they'll fall back to the IB values). Do the same for cellForRowAtIndexPath and return the [super] implementation for all sections except section 1.</p> <pre><code>@implementation myMostlyStaticTableViewController @synthesize myFancyArray; - (NSInteger) tableView:(UITableView *) tableView numberOfRowsInSection:(NSInteger) section { if (section == 1) return [myFancyArray count]; // the number of rows in section 1 else return [super tableView:tableView numberOfRowsInSection:section]; } - (UITableViewCell *) tableView:(UITableView *) tableView cellForRowAtIndexPath:(NSIndexPath *) indexPath { // for cells not in section 1, rely on the IB definition of the cell if (indexPath.section != 1) return [super tableView:tableView cellForRowAtIndexPath:indexPath]; // configure a task status cell for section 1 MyCustomTableViewCell *cell; cell = [tableView dequeueReusableCellWithIdentifier:@"myCustomCell"]; if (!cell) { // create a cell cell = [[MyCustomTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"myCustomCell"]; } cell.myCustomLabel.text = [myFancyArray objectAtIndex:indexPath.row]; return cell; } @end </code></pre> <p>And of course you need a custom cell:</p> <pre><code>@implementation MyCustomTableViewCell - (UITableViewCell *) initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { // initialize cell and add observers self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; if (!self) return self; self.clipsToBounds = YES; self.selectionStyle = UITableViewCellSelectionStyleNone; // configure up some interesting display properties inside the cell _label = [[UILabel alloc] initWithFrame:CGRectMake(20, 9, 147, 26)]; _label.font = [UIFont fontWithName:@"HelveticaNeue-Medium" size:17]; _label.textColor = [UIColor colorWithWhite:0.2 alpha:1]; [self.contentView addSubview:_label]; return self; } @end </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