Update label in custom UITableView Cell

最后都变了- 提交于 2019-12-11 09:47:42

问题


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

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