I\'m trying to work out how to set the UITableViewCellStyle
when using the new methods in iOS 6 for UITableView
.
Previously, when creating
My solution to this is to call initWithStyle: reuseIdentifier:
after I've obtained it using [self.tableView dequeueReusableCellWithIdentifier:@"cellId" forIndexPath:indexPath]
. After all, init
is just another selector, and the compiler makes no restrictions on calling it on an already initialised object. It will however complain about not using the result of calling init, so I do:
UITableViewCell* cell = [self.tableView dequeueReusableCellWithIdentifier:@"cellId" forIndexPath:indexPath];
cell = [cell initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cellId"];
I imagine this won't work in Swift...
You can avoid an extraneous subclass by using the storyboard interface builder:
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)
Bolot's answer is the correct. Simple and you don't need to create any XIB file.
I just wanted to update his answer for whoever is doing it using Swift instead of Objective-C:
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: .value1, reuseIdentifier: reuseIdentifier)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
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.
Another alternative that saves one file is to create a Nib and use registerNib:forCellReuseIdentifier:
instead.
Making the Nib is easy: Create a new .xib file in Interface Builder. Delete the default view. Add a Table View Cell object. Using the Attributes Inspector, change the style for the cell. (Here you also have the opportunity to customize the cell further by adjusting other attributes.)
Then in your table view controller's viewDidLoad
method call something like:
[self.tableView registerNib:[UINib nibWithNibName:@"StyleSubtitleTableCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"Cell"];
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>];
}