问题
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