如何激活所有部分只有一个单选按钮?

时间:2017-07-03 08:56:58

标签: ios uitableview swift3

这里我分为几个部分,每个部分都有行,具体取决于计数所以对于每一行,单选按钮都在那里,我需要为所有部分激活一个单选按钮,但我无法实现它和部分的图像将显示here,我的代码是

@IBAction func paymentRadioAction(_ sender: KGRadioButton) {
        let chekIndex = self.checkIsPaymentRadioSelect.index(of: sender.tag)
        if sender.isSelected {

        } else{
            if(chekIndex == nil){
                self.checkIsPaymentRadioSelect.removeAll(keepingCapacity: false)
                self.checkIsPaymentRadioSelect.append(sender.tag)
                self.shippingTableView.reloadData()
            }
        }
    }

这是indexpath

中行的单元格的代码
 let cell = tableView.dequeueReusableCell(withIdentifier: "shippingCell", for: indexPath) as! shippingMethodTableViewCell
            var key = self.keys[indexPath.section]
            print(key)
            var a :[Any]  = arrayss[key] as! [Any]
            var dictionary = a[indexPath.row] as! [String:Any]
            let name = dictionary["name"]
            let price = dictionary ["price"]
            cell.methodNameLabel.text = name  as! String
            cell.priceLabel.text = price as! String
            let checkIndex = self.checkIsPaymentRadioSelect.index(of: indexPath.row)
            if(checkIndex != nil){
                cell.radioButton.isSelected = true
            }else{
                cell.radioButton.isSelected = false
            }
            return cell
        }

1 个答案:

答案 0 :(得分:0)

在这里,您不必使用数组来跟踪所选的单选按钮

chekIndex作为IndexPath类型的全局变量var chekIndex:IndexPath? 并修改如下代码

   @IBAction func paymentRadioAction(_ sender: KGRadioButton) {
     let center = sender.center;
    let centralPOint = sender.superview?.convert(sender.center, to:self.shippingTableView )
    let indexPath =  self.shippingTableView.indexPathForRow(at: centralPoint)

    if sender.isSelected {

    } else{
        chekIndex = indexPath
        self.shippingTableView.reloadData()
    }
}


let cell = tableView.dequeueReusableCell(withIdentifier: "shippingCell", for: indexPath) as! shippingMethodTableViewCell
var key = self.keys[indexPath.section]
print(key)
var a :[Any]  = arrayss[key] as! [Any]
var dictionary = a[indexPath.row] as! [String:Any]
let name = dictionary["name"]
let price = dictionary ["price"]
cell.methodNameLabel.text = name  as! String
cell.priceLabel.text = price as! String
if checkIndex == indexPath {
    cell.radioButton.isSelected = true
} else {
    cell.radioButton.isSelected = false

}
return cell
}