Swift tableView.dequeueReusableCell从不返回Nil

时间:2021-10-12 21:17:47

This is my Swift code to generate my table view. I am trying to set up a tableView with detail labels. The problem is that the following code is never called.

这是我生成表格视图的Swift代码。我正在尝试设置带有详细标签的tableView。问题是永远不会调用以下代码。

if (cell == nil) {
            println("1")
            cell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "CellSubtitle")
            //cell = tableViewCell
        }

Here is the code you need for the method:

以下是该方法所需的代码:

    func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell!
{
    println("start TableViewCellForRowAtIndexPath")
    var cell: UITableViewCell! = tableView.dequeueReusableCellWithIdentifier("CellSubtitle", forIndexPath: indexPath) as UITableViewCell
    if (cell == nil) {
        println("1")
        cell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "CellSubtitle")
        //cell = tableViewCell
    }

    cell.textLabel.text = instructions[indexPath.row].text
    println("2")
    //cell.detailTextLabel
    cell.detailTextLabel.text = "HI"
    println("3")

Here is the console output for the method:

以下是该方法的控制台输出:

start load
1
2
done load
start TableViewCellForRowAtIndexPath
2
fatal error: Can't unwrap Optional.None
(lldb) 

How can I initialize the detailTextLabel in order to insert text? When I try to set the text for the label, I receive

如何初始化detailTextLabel以插入文本?当我尝试设置标签的文本时,我会收到

fatal error: Can't unwrap Optional.None. 

Why am I receiving this error?

为什么我收到此错误?

3 个解决方案

#1


6  

You don't need to check if the cell is nil using this dequeue method, as long as you've register a cell class for reuse, or provided a prototype in Interface Builder.

只要您注册一个单元类以供重用,或者在Interface Builder中提供原型,您就不需要使用此出列方法检查单元格是否为零。

let cell = tableView.dequeueReusableCellWithIdentifier("CellSubtitle", forIndexPath: indexPath) as! UITableViewCell

If however, you want to continue manually initializing the cells, you can use the following dequeue method instead. (keep in mind, this is the old way)

但是,如果要继续手动初始化单元格,则可以使用以下出列方法。 (请记住,这是旧的方式)

let cell = tableView.dequeueReusableCellWithIdentifier("CellSubtitle") as? UITableViewCell

Then as far as initializing the detailTextLabel goes, you don't have to. It's built into the cell, and by specifying that the cell's style should be subtitle, that label will be set up for you.

然后,就初始化detailTextLabel而言,您不必这样做。它内置在单元格中,通过指定单元格的样式应该是副标题,将为您设置该标签。

Note that the casts aren't necessary in Swift 2.

请注意,Swift 2中不需要强制转换。

#2


0  

Important

You must register a class or nib file using the register(:forCellReuseIdentifier:) or register(:forCellReuseIdentifier:) method before calling this method.

在调用此方法之前,必须使用寄存器(:forCellReuseIdentifier :)或register(:forCellReuseIdentifier :)方法注册类或nib文件。

As the Apple's class reference goes, you need to register the class that custom the cell before you call the dequeueReusableCell(withIdentifier:for:) method.I assumed the reason why the object is nil is that you didn't register for it?

正如Apple的类引用所示,您需要在调用dequeueReusableCell(withIdentifier:for :)方法之前注册自定义单元格的类。我假设对象为零的原因是您没有注册它?

#3


-1  

Try using this

试试这个

var cell: UITableViewCell! = tableView.dequeueReusableCellWithIdentifier("CellSubtitle", forIndexPath: indexPath) as UITableViewCell
if (cell == nil) {
cell = let cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "CellSubtitle")
}

cell.textLabel.text = instructions[indexPath.row].text
cell.detailTextLabel?.text = "HI"

#1


6  

You don't need to check if the cell is nil using this dequeue method, as long as you've register a cell class for reuse, or provided a prototype in Interface Builder.

只要您注册一个单元类以供重用,或者在Interface Builder中提供原型,您就不需要使用此出列方法检查单元格是否为零。

let cell = tableView.dequeueReusableCellWithIdentifier("CellSubtitle", forIndexPath: indexPath) as! UITableViewCell

If however, you want to continue manually initializing the cells, you can use the following dequeue method instead. (keep in mind, this is the old way)

但是,如果要继续手动初始化单元格,则可以使用以下出列方法。 (请记住,这是旧的方式)

let cell = tableView.dequeueReusableCellWithIdentifier("CellSubtitle") as? UITableViewCell

Then as far as initializing the detailTextLabel goes, you don't have to. It's built into the cell, and by specifying that the cell's style should be subtitle, that label will be set up for you.

然后,就初始化detailTextLabel而言,您不必这样做。它内置在单元格中,通过指定单元格的样式应该是副标题,将为您设置该标签。

Note that the casts aren't necessary in Swift 2.

请注意,Swift 2中不需要强制转换。

#2


0  

Important

You must register a class or nib file using the register(:forCellReuseIdentifier:) or register(:forCellReuseIdentifier:) method before calling this method.

在调用此方法之前,必须使用寄存器(:forCellReuseIdentifier :)或register(:forCellReuseIdentifier :)方法注册类或nib文件。

As the Apple's class reference goes, you need to register the class that custom the cell before you call the dequeueReusableCell(withIdentifier:for:) method.I assumed the reason why the object is nil is that you didn't register for it?

正如Apple的类引用所示,您需要在调用dequeueReusableCell(withIdentifier:for :)方法之前注册自定义单元格的类。我假设对象为零的原因是您没有注册它?

#3


-1  

Try using this

试试这个

var cell: UITableViewCell! = tableView.dequeueReusableCellWithIdentifier("CellSubtitle", forIndexPath: indexPath) as UITableViewCell
if (cell == nil) {
cell = let cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "CellSubtitle")
}

cell.textLabel.text = instructions[indexPath.row].text
cell.detailTextLabel?.text = "HI"