滚动包含多个部分的表格视图时 UIButton 状态发生变化 - Swift [英] UIButton state changing while scrolling the tableview with multiple sections - Swift

查看:19
本文介绍了滚动包含多个部分的表格视图时 UIButton 状态发生变化 - Swift的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 tableview 中有多个部分,其中包含多个自定义单元格(带有单选按钮的单元格、带有复选框的单元格和带有文本字段的单元格).问题是在带有单选按钮的部分下,在单选按钮部分下只能选择一个单选按钮.选择后,我尝试滚动,选择了多个单选按钮.非常感谢帮助.

I have multiple sections in my tableview with multiple custom cells(cell with radio button, cell with check box and cell with textfield). Problem is under section with radio button, only one radio button should be selectable under radio button section. After selecting, I have tried scrolling, multiple radio buttons are selected. Help much appreciated.

class RedioButtonCell: UITableViewCell {

var radioButtonDelegate: RedioCellDelegate?
  var cellindexPath : IndexPath?
@IBOutlet weak var btnRediouttion: UIButton?
@IBOutlet weak var lblTitle: UILabel?


override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code
}
}

行单元格方法:

  func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
 if ((qaArray[indexPath.section]["Que"] as! [String:String])["type"]!) == CONTROL
    {
        let  cell = tableView.dequeueReusableCell(withIdentifier: "RedioButtonCell", for: indexPath) as! RedioButtonCell

     cell.btnRediouttion?.tag = Int("\(indexPath.section)" +  "\(indexPath.row)")!

        cell.lblTitle!.text = String(describing: ansDetailArray[indexPath.row]["survey_answer"]!)


let deselectedImage = UIImage(named:         "Radio_Unselected")?.withRenderingMode(.alwaysTemplate)
    let selectedImage = UIImage(named: "radio_Selected")?.withRenderingMode(.alwaysTemplate)
    btnRediouttion?.setImage(deselectedImage, for: .normal)
    btnRediouttion?.setImage(selectedImage, for: .selected)
    btnRediouttion?.addTarget(self, action:    #selector(self.radioButtonTapped), for: .touchUpInside)

         cell.cellindexPath = indexPath;
        return cell
    }
}
func radioButtonTapped(_ radioButton: UIButton) {
    print("radio button tapped")
    let isSelected = !(radioButton?.isSelected)!
    radioButton?.isSelected = isSelected
    if isSelected {


    }

}

推荐答案

tableView 单元格被重用(在滚动时发生),所以你需要通过保存它的 IndexPath 和在 cellForRowAt

The tableView cells are reused ( happens when scrolling ) , so you need to keep track of the selected one by saving it's IndexPath and assign it inside cellForRowAt

//

在你的 VC 中声明这一点

declare this in your VC

 var currentIndex:IndexPath?

//

class RadioButton:UIButton {

  var indexPath:IndexPath
}

//

func radioButtonTapped(_ radioButton: RadioButton) {

    self.currentIndex = radioButton.indexPath
}

//

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

      if indexPath == currentIndex {
         // this should be selected
      }
      else {
         // Deselect this 
      }

      cell.radioButton.indexPath = indexPath 
}

这篇关于滚动包含多个部分的表格视图时 UIButton 状态发生变化 - Swift的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆