UITableViewCell & IB

Discussion in 'iOS Programming' started by RossOliver, Aug 15, 2008.

  1. RossOliver macrumors regular

    Nov 6, 2006

    What's the correct way to go about customizing a UITableViewCell in IB and hooking it up to a UITableViewController?

    I tried:

    1. Created XIB with File's Owner as myTableViewController and UITableView component.
    2. Created the myTableViewController UITableViewController subclass and hooked up the UITableView.
    3. Added a UITableViewCell to the XIB and gave it the class name of a UITableViewCell subclass I made.
    4. Set the UITableViewCell reuse identifier and customized it with an image and what not.

    myTableViewController creates the cells like so:

    -( UITableViewCell * )tableView:( UITableView * )tableView cellForRowAtIndexPath:( NSIndexPath * )indexPath
    	static NSString *identity = @"MyCustomCell";
    	MyCustomCell *cell = ( MyCustomCell * )[tableView dequeueReusableCellWithIdentifier:identity];
    	if( cell == nil )
    		cell = [[[MyCustomCell alloc] initWithFrame:CGRectZero reuseIdentifier:identity] autorelease];
    	cell.text = @"test";
    	return cell;
    I'm not sure why this doesn't work? I guess it could be because the cell initializes using alloc/init - does it need to initialize from the XIB somehow? The text "test" appears in the cell but none of the customizations I made in IB...

    Thanks for your time,

  2. ayasin macrumors 6502

    Jun 26, 2008
    I don't see where you're loading your custom cell from the NIB...it looks like you're calling the init function on the default table cell which you inherit from (which will produce the behavior you describe).
  3. RossOliver thread starter macrumors regular

    Nov 6, 2006
    Can I load a single object from an XIB? I'd rather not have to put my UITableViewCell in a single XIB all by itself...

  4. TonyKL macrumors newbie

    Aug 20, 2008
    Have you figured this out yet?

    I too am having the same issue as yourself.
    I've done it exactly the same way too. (and obviously get the same output)

    I realise that I have to load the class from nib but not sure how to do this. As you said you dont want to load the complete nib (which is already loaded as the tableview etc gets rendered)

    I just want to use the cell from the nib.
  5. pashik macrumors member

    Jul 16, 2008
    I create custom cell via class
    @interface CustomCell:UITableViewCell
    and in initWithFrame i create required elements
    -(id)initWithFrame:(CRect)frame reuseIdentifier:(NSString *)reuseIdentifier{
    --some image creation--
    --some labels creation---
    -some buttons creatipn--
    [self.contentView addSubView:myimage]
    [super layoutSubviews];
    CGRect baserect....
    determine rects for elements
  6. tacoman667 macrumors regular

    Mar 27, 2008
    Personally I have not loaded a UITableViewCell from a XIB but if you are worried about loading multiple items from a XIB then how about creating a new XIB with just the UITableViewCell in it? Make the "File's Owner" of type UITableView. There should be some way to initFromNib.
  7. RossOliver thread starter macrumors regular

    Nov 6, 2006
    This is similar to what I did - I had to put the cell in its own XIB, then set the file's owner to UIViewController and set the view of the files owner to the cell. I could then load it by using initWithNibName:bundle: and set the cell to the UIViewControllers view... not very elegant but seems to work.

  8. TonyKL macrumors newbie

    Aug 20, 2008
    LOL - This is what I ended up doing. After lots of searching and trial and error this was the best soultion. It's a shame I have to have an whole .xib per custom cell but its the best way I found. I'm sure there should be a better way unless its a bug in IB.

    Thanks for the replies and glad you got it sorted.

Share This Page