Go Back   MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Aug 15, 2008, 07:33 PM   #1
RossOliver
macrumors regular
 
Join Date: Nov 2006
UITableViewCell & IB

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
RossOliver is offline   0 Reply With Quote
Old Aug 15, 2008, 07:47 PM   #2
ayasin
macrumors 6502
 
Join Date: Jun 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).
__________________
iNeedStuff Shopping Assistant - MacLife called it "the ultimate iPhone grocery list maker".
Follow me on Twitter
Need a developer? PM me.
ayasin is offline   0 Reply With Quote
Old Aug 16, 2008, 03:32 AM   #3
RossOliver
Thread Starter
macrumors regular
 
Join Date: Nov 2006
Quote:
Originally Posted by ayasin View Post
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
RossOliver is offline   0 Reply With Quote
Old Aug 20, 2008, 05:48 AM   #4
TonyKL
macrumors newbie
 
Join Date: Aug 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.
TonyKL is offline   0 Reply With Quote
Old Aug 20, 2008, 06:44 AM   #5
pashik
macrumors member
 
Join Date: Jul 2008
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
}
pashik is offline   0 Reply With Quote
Old Aug 20, 2008, 07:11 AM   #6
tacoman667
macrumors regular
 
Join Date: Mar 2008
Quote:
Originally Posted by TonyKL View Post
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.
tacoman667 is offline   0 Reply With Quote
Old Aug 20, 2008, 08:14 AM   #7
RossOliver
Thread Starter
macrumors regular
 
Join Date: Nov 2006
Quote:
Originally Posted by tacoman667 View Post
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
RossOliver is offline   0 Reply With Quote
Old Aug 20, 2008, 08:59 AM   #8
TonyKL
macrumors newbie
 
Join Date: Aug 2008
Quote:
Originally Posted by RossOliver View Post
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.
TonyKL is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
center uitableviewcell into table frame kikko088 iPhone/iPad Programming 0 Jan 21, 2014 07:19 AM
Table Basics Question - UITableViewCell newtomac123 iPhone/iPad Programming 7 Jan 11, 2014 02:36 PM
UITableViewCell Separator Not Resizing on Rotate Sean7512 iPhone/iPad Programming 2 Sep 3, 2013 06:36 AM
Passing UIImage from UITableViewCell to another controller veliborsantic74 iPhone/iPad Programming 3 Sep 20, 2012 06:25 AM
Custom Delete UITableViewCell mdhansen5 iPhone/iPad Programming 1 Jun 1, 2012 10:41 AM

Forum Jump

All times are GMT -5. The time now is 09:54 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps