【问题标题】:How to Change the image of UIButton when selected which is on tableVIewCell. in Swift 3选择 tableVIewCell 时如何更改 UIButton 的图像。在斯威夫特 3
【发布时间】:2018-02-19 08:15:33
【问题描述】:

我在 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    
}

【问题讨论】:

  • 您忘记添加有问题的代码
  • 只是一个普通的表格视图和一个按钮。稍后我会将它实施到项目中。你能帮帮我吗? @迈克·阿尔特
  • 查看@KavyaKavita 的答案并实施
  • 请详细说明。你想在哪里更改图像。如果要使图像静态,请从 Storyboard 中更改它。否则使用 didSelectItemAt 方法

标签: ios swift xcode uitableview cocoa-touch


【解决方案1】:

在设计自定义单元格时,您可以为该按钮添加不同状态的图像

  1. 默认
  2. 已选择

当您为该按钮添加选择器时,您只需在该选择器中更改按钮的选择状态

func buttonTapped(sender : UIButton){

        sender.isSelected = !sender.isSelected


}

【讨论】:

    【解决方案2】:

    这很简单,例如您的按钮来自故事板。添加使其自定义按钮

    1) 并在cellForRowAt indexPath: 中添加tag 值。

    2) 并为该按钮设置两个图像。

    3) 最后修复选定的状态。

    查看我的代码

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

    【讨论】:

      【解决方案3】:
      1. IBOutlet 将 UIButton 放入您的单元格类(例如 TableViewCell)中。
      2. 在 ViewController 类中编写一个方法来检测 buttonTap。 例如:

        func dislikeTapped(_ sender: UIButton) { // 设置发送者需要的图片 }

      3. 在 TableView-CellForRow 方法中,将目标添加到单元格中的按钮。 例如:

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

      【讨论】:

      • 别忘了设置按钮标签
      • 嗨@Karthikeyan 我已经实现了两个答案,但没有同时工作。我已经上传了代码,你能帮帮我吗
      • 你重新加载你的tableview了吗
      • 如果 sender.selected { sender.setImage(UIImage(), forState: .normal) }else{ sender.setImage(UIImage(), forState: .normal)} 你能试试吗? ?
      • 您是否为单元格 xib 或情节提要上的按钮的不同状态(选择、突出显示等)设置了正确的图像?
      【解决方案4】:

      您还需要创建一个数组来维护按钮的状态,因为当您滚动屏幕上不可见的单元格时,它们会从内存中删除,因此如果您不保持状态,它们会再次从可见的单元格中出列将使用单元格的属性 u 出列。

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

      【讨论】:

        猜你喜欢
        • 2017-03-24
        • 2022-07-22
        • 1970-01-01
        • 1970-01-01
        • 2017-02-15
        • 1970-01-01
        • 2017-07-22
        • 2017-05-26
        • 1970-01-01
        相关资源
        最近更新 更多