UITableViewCell var “table view cell” was never mutated

荒凉一梦 提交于 2020-01-06 03:59:11

问题


In swift 2.0 when I tried to make var object of UITableViewCell custom class, swift is suggesting me to convert it to "let" because object was never mutated.

I have a UIButton on UITableViewCell but it is renamed to ABC on touchupinside method call of the button if i make its object to "let"

Refer to following piece of code:

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

result:

var nameCell was never mutated convert it to let


回答1:


Use

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

instead of

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

and change var to let in

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


来源:https://stackoverflow.com/questions/36887112/uitableviewcell-var-table-view-cell-was-never-mutated

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!