问题
I need to Update an label by clicking on an Button in the same cell. Why does my code doesn't work ?
@IBAction func actionFaveUnfave(sender: AnyObject) {
let cell = self.tableView.cellForRowAtIndexPath(sender.indexPath)
println(cell?.labelFavedTimes)
cell.labelFavedTimes = "22"}
回答1:
cell?.labelFavedTimes.text = "22"
BTW
self.tableView.cellForRowAtIndexPath(sender.indexPath)
can return nil if cell is not visible
回答2:
I solved the problem by using superview...here is my solution:
let button = sender as! UIButton
let view = button.superview!
let cell = view.superview as! TableViewCellHome
let indexPath = tableView.indexPathForCell(cell)
println(indexPath)
if(indexPath != nil){
var cell = self.tableView.cellForRowAtIndexPath(indexPath!) as! TableViewCellHome
cell.labelFavedTimes.text = favedTimesInt + " Faves"
}
回答3:
cell.labelFavedTimes.text = "22"
来源:https://stackoverflow.com/questions/31562400/update-label-in-custom-uitableview-cell