想要在单击相同的UIButton时从UITableViewCell更改正确的UIButton图像

时间:2018-02-20 06:29:55

标签: ios swift uitableview uibutton

我在UITableViewCell中有UIButton。当我在UITableViewCell中单击UIButton时,多个UIButtons图像正在改变。假设,由于Json Data,有10个UIButton显示。接下来我将点击UITableViewCell中的UIButton。因此在UIButton 1,3,5,7,9中已经改变了图像。但它应该改变一个UIButton图像。请帮助我发布codes.i只想更改UITutViewCell上的特定UIButton图像点击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)
    }
}

2 个答案:

答案 0 :(得分:2)

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

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

使用与数组中相同数量的元素,用于显示表

中的项目

例如:

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

答案 1 :(得分:1)

而不是在您的手机类中采取按钮操作取出插座。并在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)

}