HTML5   发布时间:2022-04-27  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了ios – dequeueReusableCellWithIdentifier:forIndexPath:VS dequeueReusableCellWithIdentifier:大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我读过 this question并认为我理解了两种方法间的区别,直到找到一个奇怪的例子:

设置表格视图单元格的样式为基本,标识符为故事板中的单元格,代码如下:

import UIKit

class TableViewController: UITableViewController {
    var items: [String]!

    override func viewDidLoad() {
        super.viewDidLoad()
        items = ["first","second","third"]
    }

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }

    override func tableView(tableView: UITableView,numberOfRowsInSection section: int) -> Int {
        return items.count
    }

    override func tableView(tableView: UITableView,cellForRowATindexPath indexPath: NSIndexPath) -> UITableViewCell {
        // either works fine
        let cell = tableView.dequeueReusableCellWithIdentifier("Cell")! // let cell = tableView.dequeueReusableCellWithIdentifier("Cell",forIndexPath: indexPath)

        cell.textLabel?.text = items[indexPath.row]
        return cell
    }
}

很简单,但是当我将tableView:cellForRowATindexPath:方法分别更改为1,2,3,4个时:

情况1:

override func tableView(tableView: UITableView,cellForRowATindexPath indexPath: NSIndexPath) -> UITableViewCell {
    var cell = tableView.dequeueReusableCellWithIdentifier("Cell",forIndexPath: indexPath)
    cell = tableView.dequeueReusableCellWithIdentifier("Cell",forIndexPath: indexPath)

    cell.textLabel?.text = items[indexPath.row]
    return cell
}

案例2:

override func tableView(tableView: UITableView,forIndexPath: indexPath)
    cell = tableView.dequeueReusableCellWithIdentifier("Cell")!

    cell.textLabel?.text = items[indexPath.row]
    return cell
}

案例3:

override func tableView(tableView: UITableView,cellForRowATindexPath indexPath: NSIndexPath) -> UITableViewCell {
    var cell = tableView.dequeueReusableCellWithIdentifier("Cell")!
    cell = tableView.dequeueReusableCellWithIdentifier("Cell",forIndexPath: indexPath)

    cell.textLabel?.text = items[indexPath.row]
    return cell
}

案例4:

override func tableView(tableView: UITableView,cellForRowATindexPath indexPath: NSIndexPath) -> UITableViewCell {
    var cell = tableView.dequeueReusableCellWithIdentifier("Cell")!
    cell = tableView.dequeueReusableCellWithIdentifier("Cell")!

    cell.textLabel?.text = items[indexPath.row]
    return cell
}

案例1,2(不起作用):

案例3,4(工作正常):

如何解释?我认为从另一个角度理解这两种方法确实很有帮助,任何意见都是受欢迎的.

解决方法

在每种情况下,您为每行出列两个单元格.在情况1和2中,首先调用(“Cell”,forIndexPath:indexPath)版本.在这种情况下,表格视图以每行两个单元格结束,一个完全重叠并遮挡另一个.您可以在视图检查器中看到这个,因为您可以修改视角以查看后面:

(我修改了cellForRowATindexPath代码,如下所示:

override func tableView(tableView: UITableView,cellForRowATindexPath indexPath: NSIndexPath) -> UITableViewCell {
    var cell = tableView.dequeueReusableCellWithIdentifier("plainCell",forIndexPath: indexPath)
    cell.textLabel!.text = "First cell for row \(indexPath.row)"
    cell = tableView.dequeueReusableCellWithIdentifier("plainCell",forIndexPath: indexPath)
    cell.textLabel!.text = "Second cell for row \(indexPath.row)"
    print("Cell being returned is \(cell)")
    return cell
}

为每个单元格指定不同的文本标签.)在情况3和4中,首先调用(“单元格”)版本,表视图每行只有一个单元格.

为什么不同的行为?如果您创建UITableViewCell的自定义子类并在故事板中使用它,则可以覆盖各种方法添加print()语句以查看正在发生的事情.特别是awakeFromNib,didMoveToSuperView和deinit.发生的情况是,在情况1和2中,创建了第一个单元格(awakeFromNib)并立即将其添加(didMoveToSuperView)到superview,可能是表视图或其子视图之一.在情况3和4中,创建了第一个单元格但未添加到超级视图中.相反,一段时间后,细胞被解除分配(deinit).

(注意,如果第二个单元格使用(“Cell”,forIndexPath:indexPath)版本出列,它也会立即添加到superview.但是,如果第二个单元格使用(“Cell”)版本出列,则它是仅在cellForRowATindexPath方法返回后才添加到superview.)

因此,关键的区别在于(“Cell”,forIndexPath:indexPath)版本导致单元格被立即添加表视图中,甚至在cellForRowATindexPath完成之前.这在您提到的问题/答案中暗示,因为它表明出列单元格的大小正确.

一旦添加到superview,第一个单元格就不能被释放,因为它的superview仍然有一个强引用.如果单元格以(“单元格”)版本出列,则它们不会被添加到超级视图中,因此一旦重新分配单元格变量,就没有对它们的强引用,因此它们被解除分配.

希望这一切都有意义.

大佬总结

以上是大佬教程为你收集整理的ios – dequeueReusableCellWithIdentifier:forIndexPath:VS dequeueReusableCellWithIdentifier:全部内容,希望文章能够帮你解决ios – dequeueReusableCellWithIdentifier:forIndexPath:VS dequeueReusableCellWithIdentifier:所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。