gpt4 book ai didi

ios - cellForRow(在 : ) returns nil

转载 作者:行者123 更新时间:2023-11-28 10:06:25 24 4
gpt4 key购买 nike

所以我有这个功能。

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {  
let cellIdentifier = "Cell"
let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier) as! customCell
changeCellProperty(selectedIndexPath: indexPath)
return cell;
}

func changeCellProperty(selectedIndexPath: IndexPath){
print("indexpath = \(selectedIndexPath)") . // printing [0,0] and all values
let cell = self.tableView.cellForRow(at: selectedIndexPath) as! customCell
// got nil while unwrapping error in above statement.

cell.label.text = ""
// and change other properties of cell.
}

我无法理解错误。当我获取索引路径时,为什么我无法指向特定的单元格并相应地更改属性。

最佳答案

您无法访问尚未添加到 tableView 的单元格。这就是您在 changeCellProperty 方法中尝试执行的操作。因此,如果您的出队有效,那么您需要做的就是将出队的单元格传递给该方法。

func changeCellProperty(cell: customCell){
cell.label.text = ""
// and change other properties of cell.
}

您的 cellForRowAt 方法如下所示。

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cellIdentifier = "Cell"
let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier) as! customCell
changeCellProperty(cell: cell)
return cell
}

注意:类名应该是UpperCamelCase。所以你的 customCell 应该命名为 CustomCell

关于ios - cellForRow(在 : ) returns nil,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52696362/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com