I am loading custom subclassed UITableViewCell
s 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?
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.
If you want to initialise any UIView after it's loaded from a nib, you should override initWithCoder
. awakeFromNib
might also be useful. See 'Methods to Override' in the UIView reference.
There's a method prepareForReuse
that you can use.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.