2017-02-17 43 views
0

我在CustomTableViewCells上設置了一個UIButton。在CustomTableViewCells上設置複選框

我想在選中時切換UIButton的圖像,但默認圖像在我再次選擇後不會改變。

enter image description here 類CustomCell:UITableViewCell的{

@IBOutlet weak var myButton: UIButton! 

override func awakeFromNib() { 
    super.awakeFromNib() 
    self.setup() 
} 

private func setup() { 
    self.myButton.setImage(UIImage(named: "checked"), for:.normal) 
    self.myButton.setImage(UIImage(named: "unchecked"), for:.highlighted) 

} 
+0

@ Nirav D單選支票會很好! – risa8

+0

我無法添加IBOutlet操作插件,因爲沒有我上傳的照片這樣的選項 – risa8

回答

0

您可以使用屬性highlight,並在TableView

對於實例稱爲didSelectItemAtIndexPath時,你可以更改此屬性:

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

    print("This is your Answer... \(indexPath.row)") 

    let newSelectedView = tableView.cellForItemAtIndexPath(indexPath) as! ChoiceCell 

    //important 
    newSelectedView.highlight.hidden = false 

    if self.selectedIndex != -1 && self.selectedIndex != indexPath.row { 
     let indexPathSelected = NSIndexPath(forRow: self.selectedIndex, inSection: 0) 
     let prevSelected = tableView.cellForItemAtIndexPath(indexPathSelected) as! ChoiceCell 

     //important 
     prevSelected.highlight.hidden = true 
    } 

    self.selectedIndex = indexPath.row 

}