在didSelectRowAtIndexPath中更改自定义UIImage

时间:2015-07-06 01:47:31

标签: ios swift uitableview

我的单元格中有一个自定义UIImage,我需要在调用didSelectRowAtIndexPath时更改。由于某种原因,图像没有变化,我认为这是因为我在方法中的单元格声明。

class CustomCell: UITableViewCell {

    @IBOutlet weak var indicator: UIImageView!

}

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

        var cell = tableView.dequeueReusableCellWithIdentifier("CustomCell", forIndexPath: indexPath) as! CustomCell

        cell.indicator.image = UIImage(named: "newImage")

}

单击单元格后,如何将UIImage更改为“newImage”?

3 个答案:

答案 0 :(得分:1)

从索引路径

创建单元格
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

        var cell = tableView.cellForRowAtIndexPath(indexPath) as! CustomCell

        cell.indicator.image = UIImage(named: "newImage")

}

答案 1 :(得分:0)

您需要创建一个UIImage变量,并在按下tableviewcell时更改该图像变量。

例如......

 var imageVar:UIImage = UIImage(named: "IM")

   func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCellWithIdentifier("Cell") as! UITableViewCell?

        let ImageView:UIImageView = cell!.viewWithTag(1) as! UIImageView

        ImageView.image = imageVar


}


    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

self.imageVar = UIImage(named: "Image")

self.tableView.reloadData

}

答案 2 :(得分:0)

对于Swift 5

   func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let cell = tableView.cellForRow(at: indexPath) as! MembersCell
        cell.member_status.image =  YourImage
}