Swift:使用 tableView DidSelect 方法更改单元格的 UIButton 图像 [英] Swift: Change the cell's UIButton Image With tableView DidSelect method

查看:28
本文介绍了Swift:使用 tableView DidSelect 方法更改单元格的 UIButton 图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 TableView 与两个标签和一个 UIButton 在 Tableview 内.因此,在 DidSelect 方法上,我想更改该特定单元格的 UIButton 图像.作为附件图片.

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.

推荐答案

检查这个

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!
}

输出

这篇关于Swift:使用 tableView DidSelect 方法更改单元格的 UIButton 图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆