【问题标题】:Want to change a correct UIButton image from UITableViewCell on click same UIButton想要在单击相同的 UIButton 时从 UITableViewCell 更改正确的 UIButton 图像
【发布时间】:2018-02-20 06:29:54
【问题描述】:

我在 UITableViewCell 中有 UIButton。当我单击 UITableViewCell 中的 UIButton 时,多个 UIButtons 图像正在发生变化。假设,由于 Json Data 显示了 10 个 UIButton。接下来我将点击 UITableViewCell 中的 UIButton。结果在 UIButton 1、3、5、7、9 中改变了图像。但它应该改变一个 UIButton 图像。请帮我发布代码。我只想在单击 UIButton 时从 UITableViewCell 更改特定的 UIButton 图像

TableViewController

import UIKit

class HomeViewController2: UIViewController, UITableViewDelegate,UITableViewDataSource {

    @IBOutlet weak var homeTableView: UITableView!

    let arrNewsList = [[Home]]()

    override func viewDidLoad() {
        super.viewDidLoad()

        self.homeTableView.delegate = self
        self.homeTableView.dataSource = self
    }

    func numberOfSections(in tableView: UITableView) -> Int {
        return arrNewsList.count
    }

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

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCell(withIdentifier: "HomeTableViewCell2", for: indexPath) as! HomeTableViewCell2
        return cell
    }

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {

    return self.view.frame.size.height

}
}

TableViewCell

class HomeTableViewCell2: UITableViewCell {

    @IBOutlet weak var bookMarkBtn: UIButton!

    override func awakeFromNib() {
        super.awakeFromNib()

    }

    @IBAction func bookMarkBtnClick(_ sender: Any) {

        guard let btnCheck = sender as? UIButton else {
            return
        }

        btnCheck.setImage(UIImage(named: "favorite_blue"), for: UIControlState.normal)
    }
}

【问题讨论】:

  • 你得到了什么结果?
  • 您是否为您的 json 响应创建了任何模型类对象?你能分享你的示例 json 吗?
  • 这是出列可重用单元内存引起的问题,反映了这一点。
  • @NilomiShah 尝试帮助我
  • 在您的自定义单元格中,尝试添加 prepareForReuse() 并将按钮变量设置为 nil

标签: ios swift uitableview uibutton


【解决方案1】:

这是一个问题,因为您的 TableView Cell 按钮正在重用内存。

我曾经遇到过这样的问题,你可以做的是维护一个数组。

获取与数组中相同数量的元素,用于显示表格中的项目

例如:

var arrayButton = [Int]
    for elements in mainArray {
       arrayButton.append(0)
    }

现在你的数组已经准备好了,

当按钮状态改变时改变它的值, 0 = 禁用 1:选择

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

  let cell = tableView.dequeueReusableCell(withIdentifier: "HomeTableViewCell2", for: indexPath) as! HomeTableViewCell2
  cell.bookMarkBtn.addTarget(self, action: #selector(bookMarkBtnClick(sender:)), for: .touchUpInside)

    if(arrayButton[indexPath.row] == 1) {
          cell.bookMarkBtn.setImage(UIImage(named: "set_selected_image"), for: UIControlState.normal)
       }
     else {
         cell.bookMarkBtn.setImage(UIImage(named: "set_unselected_image"), for: UIControlState.normal)
     }
     cell.bookMarkBtn.tag = indexPath.row
     return cell
  }




  func bookMarkBtnClick(sender: UIButton) {
        if(arrayButton[sender.tag] == 1){
            arrayButton[sender.tag] = 0
            sender.setImage(UIImage(named: "set_unselected_image"), for: UIControlState.normal)
         }
          else {
               arrayButton[sender.tag] = 1 
               sender.setImage(UIImage(named: "set_selected_image"), for: UIControlState.normal)
}
}

【讨论】:

  • @objc func bookMarkBtnClick(sender: UIButton) { if(arrayButton[sender.tag] == 1){ **i got error down line** // Use of unresolved identifier 'intIndexPath' arrayButton[intIndexPath] = 0 } else { arrayButton[sender.tag] = 1 } }
  • 使用未解析的标识符 intIndexPath arrayButton[intIndexPath] = 0
  • 现在看@EmmaAni
  • 我忘记改了,抱歉,现在检查
  • 朋友!按钮的图像没有改变
【解决方案2】:

不要在你的单元格类中采取按钮操作,而是采取一个出口。并在 cellForRowAt 中添加操作。

类似这样的:

class HomeTableViewCell2: UITableViewCell {

    @IBOutlet weak var bookMarkBtn: UIButton!

     override func awakeFromNib() {
         super.awakeFromNib()

     }
 }

在 cellForRowAt 中:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "HomeTableViewCell2", for: indexPath) as! HomeTableViewCell2

    cell.bookMarkBtn.tag = indexPath.row
    cell.bookMarkBtn.addTarget(self, action: #selector(bookMarkBtnClick(sender:)), for: .touchUpInside)

    return cell
}

仅在您的 ViewController 中执行此功能:

func bookMarkBtnClick(sender: UIButton) {
   sender.setImage(UIImage(named: "favorite_blue"), for: UIControlState.normal)

}

【讨论】:

  • 对不起,我没有意识到
  • 请尝试在您的答案中添加一些代码,谢谢
  • 你能展示你做了什么吗?因为同样的事情对我有用?如果您单击一个按钮,该按钮的图像应该会改变吗?
  • 问题和以前一样
  • 然后使用@NilomiShah 的想法,这是一种选择。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多