使用 iOS 6 UITableView dequeueReusableCellWithIdentifier:forIndexPath 时设置 UITableViewCell 的样式: [英] Setting style of UITableViewCell when using iOS 6 UITableView dequeueReusableCellWithIdentifier:forIndexPath:

查看:34
本文介绍了使用 iOS 6 UITableView dequeueReusableCellWithIdentifier:forIndexPath 时设置 UITableViewCell 的样式:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 iOS 6 中为 UITableView 使用新方法时如何设置 UITableViewCellStyle.

I'm trying to work out how to set the UITableViewCellStyle when using the new methods in iOS 6 for UITableView.

以前,在创建 UITableViewCell 时,我会更改 UITableViewCellStyle 枚举以在调用 initWithStyle: 时创建不同类型的默认单元格,但是从什么我可以收集,现在已经不是这样了.

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.

UITableView 的 Apple 文档指出:

The Apple documentation for UITableView states:

返回值:具有关联重用标识符的 UITableViewCell 对象.此方法始终返回有效单元格.

Return Value: A UITableViewCell object with the associated reuse identifier. This method always returns a valid cell.

讨论:出于性能原因,表视图的数据源通常应在将单元格分配给 tableView:cellForRowAtIndexPath: 方法中的行时重用 UITableViewCell 对象.表视图维护数据源已标记为重用的 UITableViewCell 对象的队列或列表.当要求为表格视图提供新单元格时,从数据源对象调用此方法.如果现有单元格可用,则此方法将其出列,或者根据您之前注册的类或 nib 文件创建一个新单元格.

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.

重要:在调用此方法之前,您必须使用 registerNib:forCellReuseIdentifier: 或 registerClass:forCellReuseIdentifier: 方法注册一个类或 nib 文件.

Important: You must register a class or nib file using the registerNib:forCellReuseIdentifier: or registerClass:forCellReuseIdentifier: method before calling this method.

如果您为指定的标识符注册了一个类并且必须创建一个新单元格,则此方法通过调用其 initWithStyle:reuseIdentifier: 方法来初始化单元格.对于基于 nib 的单元格,此方法从提供的 nib 文件加载单元格对象.如果现有单元格可供重用,则此方法会调用该单元格的 prepareForReuse 方法.

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.

这是我的新 cellForRowAtIndexPath 在实现新方法后的样子:

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

到目前为止,我的代码运行良好,但始终返回默认样式.如何更改此设置以便我可以创建具有其他样式的单元格,例如 UITableViewCellStyleDefaultUITableViewCellStyleValue1UITableViewCellStyleValue2UITableViewCellStyleSubtitle>?

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?

我不想对 UITableViewCell 进行子类化,我只想像在 iOS 6 之前一样更改默认类型.Apple 提供增强的方法但文档最少,这似乎很奇怪支持他们的实施.

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.

推荐答案

我知道你说过你不想创建子类,但这看起来是不可避免的.UITableView 根据在 iOS 6.0 模拟器中测试时的汇编代码,通过执行

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

换句话说,发送的样式 (UITableViewCellStyleDefault) 似乎是硬编码的.为了解决这个问题,您需要创建一个子类来覆盖默认的初始值设定项 initWithStyle: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;
}

此外,最好在 viewDidLoad 中发送 registerClass:forCellReuseIdentifier:,而不是每次请求单元格时都这样做:

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

这篇关于使用 iOS 6 UITableView dequeueReusableCellWithIdentifier:forIndexPath 时设置 UITableViewCell 的样式:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆