【问题标题】:UITableViewCell var "table view cell" was never mutatedUITableViewCell var "table view cell" 从未发生过变异
【发布时间】:2016-08-21 14:11:19
【问题描述】:

在 swift 2.0 中,当我尝试制作 UITableViewCell 自定义类的 var 对象时,swift 建议我将其转换为“let”,因为对象从未发生过变异。

我在 UITableViewCell 上有一个 UIButton,但如果我将它的对象设置为“让”,它会在按钮的 touchupinside 方法调用中重命名为 ABC

参考以下代码:

var nameCell: UITableViewCell = tableView.dequeueReusableCellWithIdentifier(nameCellIdentifier, forIndexPath: indexPath) 

switch indexpath.row{
case 0:
     nameCell.customButton.titleLabel!.text = "ABC"
case 1:
     nameCell.customButton.titleLabel!.text = "DEF"
case 2:
     nameCell.customButton.titleLabel!.text = "GHI"
}

return nameCell

结果:

var nameCell 从未发生变异,将其转换为 let

【问题讨论】:

  • 使用 let 代替 var 像 let nameCell
  • 但如果我使用'让'UIButton 总是在其touchUpInside 方法调用中将其title 作为“ABC”返回

标签: ios swift sdk


【解决方案1】:

使用

nameCell.customButton.setTitle("ABC", forState: .Normal)

而不是

nameCell.customButton.titleLabel!.text = "ABC"

并在

中将var更改为let
var nameCell: UITableViewCell = tableView.dequeueReusableCellWithIdentifier(nameCellIdentifier, forIndexPath: indexPath) 

【讨论】:

  • 谢谢@UditS 我正要发布同样的内容。我刚刚将其解决为titleLabel.text 是swift 的{ get } 属性,需要setTitle 属性,但奇怪的是我没有收到任何警告我无法从swift 设置get 属性
  • ObjC 中的行为相同。由于您尝试设置 titleLabel.text 这不是标签的 { get } (只读)属性,因此它不会给出任何错误。另一方面,如果您尝试将某些内容设置为titleLabel,例如titleLabel = UILabel(frame: CGRectMake(0, 0, 0, 10)),它确实会报错。
猜你喜欢
  • 2019-07-26
  • 1970-01-01
  • 2023-03-22
  • 1970-01-01
  • 2020-01-02
  • 1970-01-01
  • 2017-08-17
  • 1970-01-01
  • 2021-02-18
相关资源
最近更新 更多