Setting style of UITableViewCell when using iOS 6 UITableView dequeueReusableCellWithIdentifier:forIndexPath:
I know you said you didn't want to create a subclass, but it looks inevitable. Based on the assembly code while testing in the iOS 6.0 simulator, UITableView
creates new instances of UITableViewCell
(or its subclasses) by performing
[[<RegisteredClass> alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:<ReuseIdentifier>]
In other words, the style sent (UITableViewCellStyleDefault
) appears to be hard-coded. To get around this, you will need to create a subclass that overrides the default initializer initWithStyle:reuseIdentifier:
and passes the style you wish to use:
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
// ignore the style argument, use our own to override
self = [super initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuseIdentifier];
if (self) {
// If you need any further customization
}
return self;
}
Also, it might be better to send registerClass:forCellReuseIdentifier:
in viewDidLoad
, instead of doing it every time a cell is requested:
- (void)viewDidLoad
{
[super viewDidLoad];
[self.tableView registerClass:<RegisteredClass> forCellReuseIdentifier:<ReuseIdentifier>];
}
dequeueReusableCellWithIdentifier
isn't deprecated so you aren't required to use the new dequeueReusableCellWithIdentifier:forIndexPath:
.
Use the new way along with the appropriate register method (in viewDidLoad) if you are using a custom cell class but use the old way if you want to use one of the UITableViewCellStyle enums.
You can avoid an extraneous subclass by using the storyboard interface builder:
- In the Storyboard view, select the table view cell prototype cell (on the table view)
- In the Utilities view, in the Attributes inspector, modify the Style value
- (Optionally) Modify other values such as Selection and Accessory
The new iOS 6.0 dequeueReusableCellWithIdentifier:forIndexPath:
does use those values when allocating new cells and returning them. (Tested on an iOS 6.0 compilation using Xcode 4.5.2)