Clicky

iOS Dev Nugget 86 Using UITableView -registerClass:forCellReuseIdentifier: with UITableViewCell

.

Need to run a code review on your codebase? Hire me

In #50: UITableView -registerClass:forCellReuseIdentifier:, I wrote about using UITableView -registerClass:forCellReuseIdentifier:. But if you use UITableView, you wouldn't be able to use any style other than UITableViewCellStyleDefault. The only way to work around it is to implement a subclass for each style you want to use and pass them to -registerClass:forCellReuseIdentifier: instead. E.g. to use UITableViewCell with UITableViewCellStyleSubtitle, do this:

@interface TableViewCellStyleSubtitle : UITableViewCell
@end

@implementation TableViewCellStyleSubtitle
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString*)reuseIdentifier {
    //Hardcoded to UITableViewCellStyleSubtitle 
    if (self = [super initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuseIdentifier]) {
    }
    return self;
}
@end

and then use it like this:

[self.tableView registerClass:[TableViewCellStyleSubtitle class] forCellReuseIdentifier:@"Some Identifier"];


Your feedback is valuable: Do you want more nuggets like this?   Yes   or   No

.

.

Like this and want such iOS dev nuggets to be emailed to you, weekly?

Sign Me Up! or follow @iosdevnuggets on Twitter

.

View archives of past issues