Kai
Kai

Reputation: 1350

Subclasses of UITableViewCell in UIStoryBoard

I recently started a new project using iOS 5 with UIStoryBoards (awesome feature;)). Especially the ability to create static cells in a tableView is great. But here starts my problem. I want to customize these Cells by using a subclass of UITableViewCell and not have to customize the cell in every tableView with static content.

First I thought all that was needed was to set the class of the TableViewCell to MyCustomClass but the design is not used.

Long story short: Is there a way to use subclasses of UITableViewCell as static content in a UIStoryboard? How does the storybord instantiate the cells? (its not init() or initWithStyle())

Thank you in advance;)

Upvotes: 3

Views: 922

Answers (2)

InitJason
InitJason

Reputation: 2683

It's important to set the Identifier in the inspector panel in addition to setting your custom class. Then to retrieve your awesome new design use:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  static NSString *cellIdentifier = @"myCustomID";
  //New cell prototypes will be returned from the dequeue if none have been allocated yet.
  MySubclassCell* cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

  if(cell == nil) {
    //This shouldn't happen but you can build manually to be safe.
    cell = [[MySubclassCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
  }

  return cell;
}

Dequeueing will build you the new cell instead of needing to alloc your own.

Upvotes: 1

Eugene
Eugene

Reputation: 10045

I don't really know whether there is a way to use custom cell in interface builder (probably it isn't possible). UITableViewCells are getting initiated via initWithCoder: method, just like any other object that conforms to NSCoding protocol.

Upvotes: 2

Related Questions