【问题标题】:Swift: Change the cell's UIButton Image With tableView DidSelect methodSwift:使用 tableView DidSelect 方法更改单元格的 UIButton 图像
【发布时间】:2019-01-22 05:20:13
【问题描述】:

我有TableView,在Tableview 中有两个标签和一个UIButton。所以在DidSelect 方法上,我想更改那个特定单元格的UIButton 图像。作为附加图片。

【问题讨论】:

  • 显示你的代码,你尝试什么?
  • 您想将按钮的图像显示为选中状态吗?
  • 请详细说明您的要求。
  • 完全正确.. @JarvisTheAvenger
  • @KrunalNagvadia 你想显示选中的单选按钮吗??

标签: swift xcode uitableview uibutton


【解决方案1】:

检查一下

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

输出

【讨论】:

    【解决方案2】:

    你需要先在cellForRow方法中设置按钮标签

    didSelectRowAtIndexPath 方法之后,您需要设置按钮的图像,在didDeselectRowAtIndexPath 中设置按钮中的取消选择图像。

    您可以使用以下方法设置按钮图像:

    playButton.setImage(UIImage(named: "play.png"), forState:UIControlState.Normal)
    

    【讨论】:

      【解决方案3】:
      func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
      
              guard let btn = (tableView.cellForItem(at: indexPath) as! yourCellName).button else {
                  return
              }
              btn.setImage(UIImage(named: "yourSelectedImage name"), for: .normal)
          }
          func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
              guard let btn = (tableView.cellForItem(at: indexPath) as! yourCellName).button else {
                  return
              }
              btn.setImage(UIImage(named: "yourNotSelectedImagename"), for: .normal)
          }
      

      【讨论】:

      • 我想知道您为什么要对这个答案投反对票吗?它对我来说很好。
      • 我认为这是最好的方法。无需检查任何变量。
      • 你的代码的问题是当用户滚动你的选择时会消失。
      • 我们可以通过cellForRowAt 中的标志来避免这个滚动问题
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多