2017-09-11 37 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

相关问题