Posted By

BenClayton on 02/05/10


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite


load a reusable UITableViewCell from a XIB

 / Published in: Objective C

  1. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  2. UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"CustomCellId"];
  3. if (cell == nil) {
  4. UIViewController *c = [[UIViewController alloc]
  5. initWithNibName:@"CustomCell" bundle:nil];
  7. cell = (PostCell *)c.view;
  8. [c release];
  9. }
  10. return cell;
  11. }

Report this snippet  


RSS Icon Subscribe to comments
Posted By: BenClayton on February 5, 2010

In IB, start an empty XIB. Now go to the pallete and drag in a UITableViewCell, double click to bring up and edit.

include only the custom UITableViewCell (no other UIViews or other top level controls) - make sure it's a real UITableViewCell in IB, or you cannot set a reuse identifier (as opposed to casting a UIView in IB as your custom UITableViewCell class). Then you can add lables or whatever you like within the cell, as well as setting the reuse identifier or set whatever disclosure indicator you might like.

To use, you provide code like this in the tableView:cellForRow:atIndexPath: method:

If you have any labels or other controls you want to reference in your code, wire them in IB to your custom cell class - NOT the file's owner, which you do not ever need to set using the above code (you can leave it as NSObject).

Edit: I note you are really looking for an NSCell answer, but the code approach to using IB should be identical in Cocoa with the Cocoa Touch code I used above as loadNibNamed is a standard Cocoa call.

You need to login to post a comment.