Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

RossOliver

macrumors regular
Original poster
Nov 6, 2006
157
0
Hey,

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:

Code:
-( 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,

-Ross
 

ayasin

macrumors 6502
Jun 26, 2008
318
0
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).
 

RossOliver

macrumors regular
Original poster
Nov 6, 2006
157
0
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).

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...

-Ross
 

TonyKL

macrumors newbie
Aug 20, 2008
15
2
London, UK
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.
 

pashik

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

tacoman667

macrumors regular
Mar 27, 2008
143
0
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.

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.
 

RossOliver

macrumors regular
Original poster
Nov 6, 2006
157
0
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.

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.

-Ross
 

TonyKL

macrumors newbie
Aug 20, 2008
15
2
London, UK
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.

-Ross

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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.