I am implementing a like dislike button on the tableview cell but unable to change the image of the button. can anybody help me out using swift 3
我正在tableview单元格上实现一个类似的不喜欢按钮,但无法更改按钮的图像。任何人都可以帮助我使用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
}
2 个解决方案
#1
1
While designing your custom cell, you can add image for that button for different states
在设计自定义单元格时,您可以为该按钮添加不同状态的图像
- Default
- 默认
- Selected
- 选
When you add selector for that button, in that selector you just need to change button's selection state
为该按钮添加选择器时,在该选择器中只需更改按钮的选择状态即可
func buttonTapped(sender : UIButton){
sender.isSelected = !sender.isSelected
}
#2
0
- IBOutlet the UIButton into your cell class(Eg. TableViewCell).
- IB将UIButton引入您的单元类(例如TableViewCell)。
-
Write a method in ViewController class to detect buttonTap. Eg:
在ViewController类中编写一个方法来检测buttonTap。例如:
func dislikeTapped(_ sender: UIButton) { // set required image to sender }
func dislikeTapped(_ sender:UIButton){//将所需图像设置为发件人}
-
In TableView-CellForRow method add target to the button in cell. Eg:
在TableView-CellForRow方法中,将目标添加到单元格中的按钮。例如:
cell.dislikeButton.addTarget(self, action: #selector(self.dislikeTapped(_:), for: .touchUpInside)
cell.dislikeButton.addTarget(self,action:#selector(self.dislikeTapped(_ :),for:.touchUpInside)
#1
1
While designing your custom cell, you can add image for that button for different states
在设计自定义单元格时,您可以为该按钮添加不同状态的图像
- Default
- 默认
- Selected
- 选
When you add selector for that button, in that selector you just need to change button's selection state
为该按钮添加选择器时,在该选择器中只需更改按钮的选择状态即可
func buttonTapped(sender : UIButton){
sender.isSelected = !sender.isSelected
}
#2
0
- IBOutlet the UIButton into your cell class(Eg. TableViewCell).
- IB将UIButton引入您的单元类(例如TableViewCell)。
-
Write a method in ViewController class to detect buttonTap. Eg:
在ViewController类中编写一个方法来检测buttonTap。例如:
func dislikeTapped(_ sender: UIButton) { // set required image to sender }
func dislikeTapped(_ sender:UIButton){//将所需图像设置为发件人}
-
In TableView-CellForRow method add target to the button in cell. Eg:
在TableView-CellForRow方法中,将目标添加到单元格中的按钮。例如:
cell.dislikeButton.addTarget(self, action: #selector(self.dislikeTapped(_:), for: .touchUpInside)
cell.dislikeButton.addTarget(self,action:#selector(self.dislikeTapped(_ :),for:.touchUpInside)