2017-09-11 36 views
0

我在桌面視圖單元上實現了一個喜歡不喜歡的按鈕,但無法更改按鈕的圖像。任何人可以幫助我使用SWIFT 3如何在tableVIewCell上選中時更改UIButton的圖像。在Swift 3

import UIKit 

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate { 


    @IBOutlet weak var tableView: UITableView! 
    var array: [String] = ["A","B","C","D","E","F"] 
    override func viewDidLoad() { 
     super.viewDidLoad() 

    } 
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return array.count 
    } 
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     let cell: cell_TableTableViewCell = self.tableView.dequeueReusableCell(withIdentifier: "cell_TableTableViewCell") as UITableViewCell! as! cell_TableTableViewCell! 

      cell.textLabel?.text = array[indexPath.row] 

     cell.button_Outlet.tag = indexPath.row 
     cell.button_Outlet.addTarget(self, action: "LikePressed", for: .touchUpInside) 

     return cell 
    } 

} 


func LikePressed(sender : UIButton){ 

    sender.isSelected = !sender.isSelected  
} 
+1

你忘了有問題 –

+0

添加代碼只是一個正常表單視圖和一個按鈕,稍後我會將它實現到項目中,可以幫助我@Mike Alter –

+0

查看@KavyaKavita的答案並執行它 –

回答

1

在設計您的自定義單元格,您可以爲按鈕不同狀態

  1. 默認
  2. 選擇

當您添加圖像爲該按鈕添加選擇器,在該選擇器中,您只需更改按鈕的選擇狀態

func buttonTapped(sender : UIButton){ 

     sender.isSelected = !sender.isSelected 


} 
0
  1. IBOutlet將UIButton放入您的單元格類(例如, TableViewCell)。
  2. 在ViewController類中編寫一個方法來檢測buttonTap。 例如:

    FUNC dislikeTapped(_發件人:的UIButton){// 組所需的圖像以發送者 }

  3. 在TableView中-CellForRow方法添加目標按鈕在細胞。 如:

    cell.dislikeButton.addTarget(自我,動作:#selector(self.dislikeTapped(_ :)爲:.touchUpInside)

+0

不要忘記設置按鈕標籤 – karthikeyan

+0

hi @ Karthikeyan我有im解答了兩個答案,但兩者都不起作用。我已經上傳了代碼,你可以幫我 –

+0

你重新加載你的tableview – karthikeyan

相關問題