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

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

问题描述

我正在尝试使用iOS 6中的新方法设置 UITableViewCellStyle UITableView

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

以前,在创建 UITableViewCell 时,我会更改 UITableViewCellStyle enum在调用 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.

讨论
出于性能原因,表视图的数据源通常应该重用UITableViewCell对象当它将单元格分配给其tableView:cellForRowAtIndexPath:方法中的行时。表视图维护数据源已标记为可重用的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;
}

到目前为止我的代码工作正常,但总是返回默认样式。如何更改此设置以便我可以使用其他样式创建单元格,例如 UITableViewCellStyleDefault UITableViewCellStyleValue1 UITableViewCellStyleValue2 UITableViewCellStyleSubtitle

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.

推荐答案

我知道你说你不想创建一个子类,但看起来不可避免。在iOS 6.0模拟器中测试时基于汇编代码, UITableView 创建 UITableViewCell (或其子类)的新实例通过执行

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

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

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时设置UITableViewCell的样式UITableView dequeueReusableCellWithIdentifier:forIndexPath:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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