Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

xcode - Swift: Change the cell's UIButton Image With tableView DidSelect method

I Have TableView with the two label and one UIButton inside the Tableview. So on DidSelect method i want to change the UIButton Image of that particular cell. As attach image.

enter image description here

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Check This

enter image description here

import UIKit

class ButtonTblViewController: UIViewController,UITableViewDelegate, UITableViewDataSource {

    var arry = ["Montitanki Chowk","Greenland Chokdi"]
    var SelectData = [NSMutableDictionary]()

    @IBOutlet weak var tbleVw: UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()

        for i in 0..<arry.count
        {
            self.SelectData.append(["data":arry[i],"isSelect":"NO"])
        }

        self.tbleVw.reloadData()
        // Do any additional setup after loading the view.
    }

    // MARK - tableView Delegates

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return SelectData.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell:listTble = tableView.dequeueReusableCell(withIdentifier: "CellID", for: indexPath as IndexPath) as! listTble
        cell.lblTit.text =  self.SelectData[indexPath.row].value(forKey: "data") as? String

        cell.btnRdo.tag = indexPath.row
        let tapgesture = UITapGestureRecognizer(target: self , action: #selector(self.sectionTapped(_:)))
        cell.btnRdo.addGestureRecognizer(tapgesture)

        let selData = self.SelectData[indexPath.row].value(forKey: "isSelect") as! NSString

        if selData == "NO" {
            cell.btnRdo.setImage( UIImage(named: "btnUnSel"), for: .normal)
        } else {
            cell.btnRdo.setImage( UIImage(named: "btnSel"), for: .normal)
        }

        return cell
    }

    @objc func sectionTapped(_ sender: UITapGestureRecognizer){
        if(self.SelectData[(sender.view?.tag)!].value(forKey: "isSelect") as! String == "NO"){
            self.SelectData[(sender.view?.tag)!].setValue("YES", forKey: "isSelect")
        }else{
            self.SelectData[(sender.view?.tag)!].setValue("NO", forKey: "isSelect")
        }
        self.tbleVw.reloadData()
    }

    /*
     // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}

class listTble: UITableViewCell {
    @IBOutlet weak var lblTit: UILabel!
    @IBOutlet weak var btnRdo: UIButton!
}

Out Put

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share

2.1m questions

2.1m answers

62 comments

56.6k users

...