I'm trying to work out how to set the UITableViewCellStyle
when using the new methods in iOS 6 for UITableView
.
Previously, when creating a UITableViewCell
I would change the UITableViewCellStyle
enum to create different types of default cells when calling initWithStyle:
but from what I can gather, this is no longer the case.
The Apple documentation for UITableView
states:
Return Value: A UITableViewCell object with the associated reuse identifier. This method always returns a valid cell.
Discussion: For performance reasons, a table view's data source should generally reuse UITableViewCell objects when it assigns cells to rows in its tableView:cellForRowAtIndexPath: method. A table view maintains a queue or list of UITableViewCell objects that the data source has marked for reuse. Call this method from your data source object when asked to provide a new cell for the table view. This method dequeues an existing cell if one is available or creates a new one based on the class or nib file you previously registered.
Important: You must register a class or nib file using the registerNib:forCellReuseIdentifier: or registerClass:forCellReuseIdentifier: method before calling this method.
If you registered a class for the specified identifier and a new cell must be created, this method initializes the cell by calling its initWithStyle:reuseIdentifier: method. For nib-based cells, this method loads the cell object from the provided nib file. If an existing cell was available for reuse, this method calls the cell’s prepareForReuse method instead.
This is how my new cellForRowAtIndexPath
looks after implementing the new methods:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *cellIdentifier = @"cell_identifier"; [tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:cellIdentifier]; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath]; return cell; }
The code I have so far works fine but always returns the default style. How can I change this so I can create cells with the other styles such as UITableViewCellStyleDefault
, UITableViewCellStyleValue1
, UITableViewCellStyleValue2
and UITableViewCellStyleSubtitle
?
I don't want to subclass UITableViewCell
, I just want to change the default type as I could do prior to iOS 6. It seems odd that Apple would provide enhanced methods but with minimal documentation to support their implementation.
Has anyone mastered this, or run in to a similar problem? I'm struggling to find any reasonable information at all.
dequeueReusableCellWithIdentifier: Returns a reusable table-view cell object after locating it by its identifier.
dequeueReusableCell(withIdentifier:)Returns a reusable table-view cell object after locating it by its identifier.
The reuse identifier is associated with a UITableViewCell object that the table-view's delegate creates with the intent to reuse it as the basis (for performance reasons) for multiple rows of a table view. It is assigned to the cell object in init(frame:reuseIdentifier:) and cannot be changed thereafter.
UITableView manages the basic appearance of the table, but your app provides the cells ( UITableViewCell objects) that display the actual content. The standard cell configurations display a simple combination of text and images, but you can define custom cells that display any content you want.
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>]; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With