uiableviewcell中的UIbutton在滚动时不响应

时间:2018-08-15 12:24:38

标签: ios iphone swift

我有一个带有自定义单元格的uitableview,其中需要根据部分中的类型显示一些复选框和单选按钮。在cellForRowAt中,我的代码是

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
                    let cell = tableView.dequeueReusableCell(withIdentifier: "PopupCell") as! PopupCell
                   //   self.mainTbl.beginUpdates()
                    cell.lblHeading.text = self.jsonNew[indexPath.section]["section_attributes_topping"][indexPath.row].stringValue
                    cell.lblPrice.text = "€"+self.jsonNew[indexPath.section]["section_attributes_price_topping"][indexPath.row].stringValue
                    cell.radioButton.tag = self.jsonNew[indexPath.section]["id"][indexPath.row].intValue


                    if self.jsonNew[indexPath.section]["type_attr"].stringValue == "radio"{
                        cell.radioButton.setImage(deselectedRadioImage, for: .normal)
                        cell.initCellItem()
                        cell.delegate = self


                    }else{
                        cell.radioButton.setImage(deselectedCheckboxImage, for: .normal)
                        cell.initCellCheckbox()
                        cell.delegate = self
                        cell.delegate_one = self
                    }

                    //cell.clipsToBounds = true
                  print(indexPath)
                   // self.mainTbl.endUpdates()
                    return cell
                }          // self.mainTbl.endUpdates()
    return cell
}

如果没有滚动,则工作正常,但是当我向下滚动并选择然后选择了错误的按钮时。 我是ios开发的新手,我知道这是单元重用的问题,但是我该如何解决。我做了谷歌搜索,但没有得到正确的答案。

我的手机代码

                //
            //  PopupCell.swift
            //  AppAndEat
            //
            //  Created by Ravi Thakur on 11/08/18.
            //  Copyright © 2018 DevelopersGroup. All rights reserved.
            //

            import UIKit
            struct Product {
                var price = 0
                var name = ""
                var attr = ""
                var heading = ""
            }

            protocol PopupCellDelegate {
                func didToggleRadioButton(_ indexPath: IndexPath)
            }


            class PopupCell: UITableViewCell {

                var product : Product!

                private var counterValue = 1
                var productIndex = 0

                @IBOutlet weak var lblQty: UILabel!
                @IBOutlet weak var btnMinus: UIButton!
                @IBOutlet weak var btnPLusg: UIButton!
                var delegate: PopupCellDelegate?
                var delegate_one: PopupCellGetValue?
                @IBOutlet weak var lblPrice: UILabel!

                @IBOutlet weak var lblHeading: UILabel!
                @IBOutlet weak var radioButton: UIButton!
                override func awakeFromNib() {
                    super.awakeFromNib()
                    // Initialization code
                }

                override func setSelected(_ selected: Bool, animated: Bool) {
                    super.setSelected(selected, animated: animated)

                    // Configure the view for the selected state
                }
                func initCellItem() {

                    let deselectedImage = UIImage(named: "circle-shape-outline (1)")?.withRenderingMode(.alwaysTemplate)
                    let selectedImage = UIImage(named: "dot-and-circle (1)")?.withRenderingMode(.alwaysTemplate)
                    radioButton.setImage(deselectedImage, for: .normal)
                    radioButton.setImage(selectedImage, for: .selected)

                    radioButton.addTarget(self, action: #selector(self.radioButtonTapped), for: .touchUpInside)
                }
                func initCellCheckbox(){
                    let deselectedCheckBoxImage = UIImage(named: "blank-check-box")?.withRenderingMode(.alwaysTemplate)
                    let selectedCheckboxImage = UIImage(named: "check")?.withRenderingMode(.alwaysTemplate)
                    radioButton.setImage(deselectedCheckBoxImage, for: .normal)
                    radioButton.setImage(selectedCheckboxImage, for: .selected)
                    radioButton.addTarget(self, action: #selector(self.checkButtonTapped), for: .touchUpInside)
                }
                @objc func radioButtonTapped(_ radioButton: UIButton) {

                    let isSelected = !self.radioButton.isSelected
                    self.radioButton.isSelected = isSelected
                    if isSelected {
                        deselectOtherButton()
                    }
                    let tableView = self.superview as! UITableView
                    let tappedCellIndexPath = tableView.indexPath(for: self)!

                    delegate?.didToggleRadioButton(tappedCellIndexPath)
                   // delegate_one?.getAllValue(product: product, indexPath: tappedCellIndexPath)
                }

                func deselectOtherButton() {
                    let tableView = self.superview as! UITableView
                    let tappedCellIndexPath = tableView.indexPath(for: self)!
                    let indexPaths = tableView.indexPathsForVisibleRows
                    for indexPath in indexPaths! {
                        if indexPath.row != tappedCellIndexPath.row && indexPath.section == tappedCellIndexPath.section {
                            let cell = tableView.cellForRow(at: IndexPath(row: indexPath.row, section: indexPath.section)) as! PopupCell
                            cell.radioButton.isSelected = false
                        }
                    }
                }
                //function for checkboxes
                @objc func checkButtonTapped(_ radioButton: UIButton) {

                    let isSelected = !self.radioButton.isSelected
                    self.radioButton.isSelected = isSelected
                    if isSelected {
                        print(self.radioButton.isSelected)
                        self.radioButton.isSelected = isSelected
                        self.btnMinus.isHidden = false
                        self.btnPLusg.isHidden = false
                        self.lblQty.isHidden = false

                    }else{
                        self.btnMinus.isHidden = true
                        self.btnPLusg.isHidden = true
                        self.lblQty.isHidden = true
                        counterValue = 1
                        self.lblQty.text = "\(counterValue)"


                       // self.radioButton.isSelected = !isSelected
                    }
                    let tableView = self.superview as! UITableView
                    let tappedCellIndexPath = tableView.indexPath(for: self)!
                    delegate?.didToggleRadioButton(tappedCellIndexPath)

                }

                //function for plus minus qty

                @IBAction func clickMinus(_ sender: Any) {
                    if(counterValue != 1){
                        counterValue -= 1;
                    }
                    self.lblQty.text = "\(counterValue)"

                    let tableView = self.superview as! UITableView
                    let tappedCellIndexPath = tableView.indexPath(for: self)!
                    //delegate?.didToggleRadioButton(tappedCellIndexPath)
                    delegate?.checkPrice(tappedCellIndexPath, price: "\(counterValue)")
                }

                @IBAction func clickPlus(_ sender: Any) {
                    counterValue += 1;
                    self.lblQty.text = "\(counterValue)"
                }
            }

1 个答案:

答案 0 :(得分:0)

看起来像

let tableView = self.superview as! UITableView let tappedCellIndexPath = tableView.indexPath(for: self)! 不会为您返回正确的indexPath。

创建类似的内容

protocol PopupCellDelegate: class {
func didTap(_ cell: Cell)

}

PopupCell类:UITableViewCell {

weak var delegate: PopupCellDelegate?
@IBAction func radioButtonPressed(_ sender: Any) {
    delegate?.didTap(self)
}

}

ViewController类:UIViewController,PopupCellDelegate {

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = ...
    cell.delegate = self
    return cell
}

func didTap(_ cell: Cell) {
    let indexPath = self.tableView.indexPath(for: cell)
    cell.delegate?.didToggleRadioButton(indexPath)
}

为您的单元格和动作重新构建它,它应该可以工作。如果没有,那么我会考虑更多)