Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
309 views
in Technique[技术] by (71.8m points)

ios6 - Setting style of UITableViewCell when using iOS 6 UITableView dequeueReusableCellWithIdentifier:forIndexPath:

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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>];
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...