UITableView单元在初始化时不是零 [英] UITableView cells not nil at initialization

查看:122
本文介绍了UITableView单元在初始化时不是零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用storyboard编辑器设置我的 UITableView 。为了创建我的单元格,我使用的是标准委托方法:

I am setting up my UITableView using storyboard editor. For creating my cells I am using the standard delegate method:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SearchResultCell"];
        if (cell == nil)
        {
        // Do cell setup
        }
    // etc
    return cell;
}

除非单元格第一次出列而不是nil,因为它应该。所以if语句中的代码永远不会被执行。

Except when the cell is dequeued the very first time it's not nil, as it should be. So the code inside the if statement is never executed.

当他们的重用标识符不一致时,人们会收到此错误,所以我继续验证我使用完全相同的在我的故事板视图中重用标识符,就像我在代码中一样。仍然面临着这个问题。我在项目中也有几个表视图,每个都有一个唯一的重用标识符。仍然没有骰子。任何人都知道其他任何可能出错的地方吗?

People get this error when their reuse identifiers are inconsistent, so I went ahead and verified that I am using the exact same reuse identifier in my storyboard views as I do in my code. Still facing the issue. I also have several tableviews within the project and each one has a unique reuse identifier. Still no dice. Anyone know anything else that could be wrong here?

推荐答案

这不再是UITableView的工作方式了。阅读你的问题,我想你可能也会对它之前的工作方式感到困惑。如果没有,抱歉,第一部分只是审查。 :)

That's not how UITableView works anymore. Reading your question, I think you might be confused about how it worked before as well. If not, sorry, the first part of this is just review. :)

以下是它的工作原理:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
    // If the tableview has an offscreen, unused cell of the right identifier
    // it will return it.
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SearchResultCell"];
    if (cell == nil)
    {
        // Initial creation, nothing row specific.
    }

    // Per row setup here.

    return cell;
}

在使用重用标识符创建单元格时,执行只有这里的初始设置。没有特定于此特定行/ indexPath的任何内容。

Here when you create the cell using the reuse identifier, you do only the initial setup here. Nothing specific to this particular row/indexPath.

在我放置每行设置注释的情况下,您有一个具有正确标识符的单元格。它可以是新鲜细胞,也可以是再生细胞。您负责与此特定行/ indexPath相关的所有设置。

Where I've put the Per row setup comment you have a cell of the right identifier. It may be a fresh cell, or a recycled cell. You're responsible for all setup related to this particular row/indexPath.

示例:如果您在某些行(可能)中设置文本,则需要设置或清除它在所有行中,或者您设置的行中的文本将泄漏到您不设置的单元格。

Example: if you set the text in some rows (likely) you need to set or clear it in all rows, or text from rows you set will leak through to cells you don't.

使用故事板,故事板和表格视图处理初始单元格创建!这是很棒的东西。在使用故事板时,您可以直接在tableview中绘制单元格原型,Cocoa Touch将为您进行初始创建。

With storyboards, though, the storyboard and table view handle the initial cell creation! This is brilliant stuff. You map out your cell prototypes directly in the tableview when using storyboards, and Cocoa Touch will do the initial creation for you.

相反,您可以得到:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SearchResultCell"];
    // You'll always have a cell now!

    // Per row setup here.

    return cell;
}

您负责与以前相同的每行设置,但是您不需要编写代码来构建你的初始空单元格,无论是内联还是自己的子类。

You're responsible for all the same per row setup as before, but you shouldn't need to write code to build your initial empty cell, either inline or in its own subclass.

正如下面的Ian所述,你仍然可以使用旧的方法。只需确保不在故事板中包含指定标识符的单元格原型。视图控制器将无法从单元格原型构建您的单元格, dequeueReusableCellWithIdentifier 将返回nil,并且您将完全处于以前的位置。

As Ian notes below, you can still use the old approach. Just make sure not to include a cell prototype in the storyboard for the identifier you specify. The view controller won't be able to build your cell from the cell prototype, dequeueReusableCellWithIdentifier will return nil, and you'll be exactly where you were before.

这篇关于UITableView单元在初始化时不是零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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