2015-07-06 86 views
1

我在自己的單元中有一個自定義UIImage,我需要在調用didSelectRowAtIndexPath時進行更改。出於某種原因,圖像沒有改變,我認爲這是因爲我的單元格聲明在方法中。在didSelectRowAtIndexPath中更改自定義UIImage

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」?

回答

1

從創建索引路徑

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

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

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

} 
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 

} 
相關問題