When using registerNib:forCellReuseIdentifier, how do I init UITableViewCell iVars?

I am loading custom subclassed UITableViewCells from nib.

My view controller viewDidLoad method uses registerNib:forCellReuseIdentifier. My view controller's cellForRowAtIndexPath method uses dequeueReusableCellWithIdentifier to load the cell, but I never alloc/init the cell.

ViewController:

- (void)viewDidLoad
{
    [super viewDidLoad];

    [self.tV registerNib:[UINib nibWithNibName:@"VersatileIntTFCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:kTFInt];
    self.tV.delegate=self;
    self.tV.dataSource=self;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Int TextField";
    VersatileIntTFCell *cell = (VersatileIntTFCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];   
    return cell;

}

The table view appears fine, and the cells appear fine.

Now, I am starting to write the subclasses of UITableViewCell. My subclasses each have some @property iVars (they are not IBOutlets). I would like to init these in the init method of the UITableViewCell subclass, but that doesn't appear to ever get called. I can init them in the viewController's cellForRowAtIndexPath but I want to avoid this. I want to do it in the UITableViewCell subclass. Is this possible? If so, how/where/when?

Answers


Override the awakeFromNib method and put your code there rather than in the init.

From the docs:

The nib-loading infrastructure sends an awakeFromNib message to each object recreated from a nib archive, but only after all the objects in the archive have been loaded and initialized. When an object receives an awakeFromNib message, it is guaranteed to have all its outlet and action connections already established. You must call the super implementation of awakeFromNib to give parent classes the opportunity to perform any additional initialization they require.


Need Your Help

How does Ninject WCF Extensions TimeService sample work?

c# wcf visual-studio-2010 ninject-2

I have a .Net 4.0 WCF service that I'm trying to setup ninject for. I downloaded the WCF extension for ninject and had a look through the TimeService example. Everything looks simple enough, howeve...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.