简体   繁体   中英

How to Change the image of UIButton when selected which is on tableVIewCell. in Swift 3

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

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    
}

While designing your custom cell, you can add image for that button for different states

  1. Default
  2. 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


}
  1. IBOutlet the UIButton into your cell class(Eg. TableViewCell).
  2. Write a method in ViewController class to detect buttonTap. Eg:

    func dislikeTapped(_ sender: UIButton) { // set required image to sender }

  3. In TableView-CellForRow method add target to the button in cell. Eg:

    cell.dislikeButton.addTarget(self, action: #selector(self.dislikeTapped(_:), for: .touchUpInside)

It's very simple, for example your button is from storyboard. Add make that it custom button .

1) And add tag value in cellForRowAt indexPath: .

2) And set two images for that butoon.

3) Finally fix selected sate.

See my code

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]
    //Step 1
    cell.button_Outlet.tag = indexPath.row
    cell.button_Outlet.addTarget(self, action: "LikePressed", for: .touchUpInside)
    //Step 2
    cell.button_Outlet.setImage(UIImage(named: "LikeImage"), for: .selected)
    cell.button_Outlet.setImage(UIImage(named: "DisLikeImage"), for: .selected)
    return cell
}

//Step 3
func btnPressed(sender:UIButton) {
    if sender.isSelected == false {
        sender.isSelected = true
    } else {
       sender.isSelected = false
    }
}

You need to create an array too to maintain the state of button as when you scroll cell that are not visible on screen are removed from memory, so they are dequeued again from one that are visible, if you not maintain the state it will use attributes of cell u dequeued from.

var arrSelectedButtonswitTag = [Int]()
    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]
    //Step 1
    cell.button_Outlet.tag = indexPath.row
    cell.button_Outlet.addTarget(self, action: "LikePressed", for: .touchUpInside)
    //Step 2
    cell.button_Outlet.setImage(UIImage(named: "LikeImage"), for: .selected)
    cell.button_Outlet.setImage(UIImage(named: "DisLikeImage"), for: .normal)
    if arrSelectedButtonswitTag.contain(sender.tag)
        sender.isSelected = false
    arrSelectedButtonswitTag.remove(sender.tag)
    } else {
        sender.isSelected = true
    arrSelectedButtonswitTag.appen(sender.tag)
    }
    return cell
}

//Step 3
func btnPressed(sender:UIButton) {
if arrSelectedButtonswitTag.contain(sender.tag)
        sender.isSelected = false
    arrSelectedButtonswitTag.remove(sender.tag)
    } else {
        sender.isSelected = true
    arrSelectedButtonswitTag.append(sender.tag)
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM