在这里,我实现了自定义设计,如图所示,我需要根据我的 Web 服务数据激活单选按钮。在 Web 服务中,我只收到一个键值对 0 和 1,如果它是 1,那么我需要激活单选按钮任何帮助如何实现?
这是我的单选按钮的代码
@IBAction func selectRadioButton(_ sender: KGRadioButton) {
let chekIndex = self.checkIsRadioSelect.index(of: sender.tag)
let checkIndex = self.checkIsButtonEnable.index(of: sender.tag)
if sender.isSelected {
} else{
if(chekIndex == nil){
self.checkIsRadioSelect.removeAll(keepingCapacity: false)
self.checkIsRadioSelect.append(sender.tag)
self.checkIsButtonEnable.removeAll(keepingCapacity: false)
self.checkIsButtonEnable.append(sender.tag)
self.tableDetails.reloadData()
}
}
}
这是表视图的代码
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! addressTableViewCell
tableDetails.isHidden = false
myActivityIndicator.stopAnimating()
let arr = detailsArray[indexPath.row]
cell.nameLabel.text = arr["name"]as? String
cell.addressLabel.text = arr["address"]as? String
let mobilenumber : Int = arr["number"] as! Int
cell.mobileNumberLabel.text = String(describing: mobilenumber)
let defaultaddress : Int = arr["default"] as! Int
cell.radioButton.tag = indexPath.row
cell.editButton.tag = indexPath.row
cell.deleteButton.tag = indexPath.row
cell.editButton.isHidden = true
cell.deleteButton.isHidden = true
let checkIndex = self.checkIsRadioSelect.index(of: indexPath.row)
if(checkIndex != nil){
cell.radioButton.isSelected = true
cell.editButton.isHidden = false
cell.deleteButton.isHidden = false
}else{
cell.radioButton.isSelected = false
cell.editButton.isHidden = true
cell.deleteButton.isHidden = true
}
return cell
在您的cellForRowAtIndexPath
方法中,更新如下所示的代码
我的建议是在重用tableView单元格时不要将indexPath.row设置为标签,它不会给你更好的结果。制作一些唯一的ID作为标签。或者做一些数学计算,用数字加/乘
。 let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! addressTableViewCell
tableDetails.isHidden = false
myActivityIndicator.stopAnimating()
let arr = detailsArray[indexPath.row]
cell.nameLabel.text = arr["name"]as? String
cell.addressLabel.text = arr["address"]as? String
let mobilenumber : Int = arr["number"] as! Int
cell.mobileNumberLabel.text = String(describing: mobilenumber)
cell.radioButton.tag = indexPath.row
cell.editButton.tag = indexPath.row
cell.deleteButton.tag = indexPath.row
cell.editButton.isHidden = true
cell.deleteButton.isHidden = true
cell.radioButton.isSelected = false
if let isDefault = arr["default"] as? Bool { // i think it won't be Integer it will be Bool,please debug & check it
cell.radioButton.isSelected = isDefault
cell.editButton.isHidden = false
cell.deleteButton.isHidden = false
}
let checkIndex = self.checkIsRadioSelect.index(of: indexPath.row)
if(checkIndex != nil){
cell.radioButton.isSelected = true
cell.editButton.isHidden = false
cell.deleteButton.isHidden = false
}
更改下面的代码行。
let checkIndex = self.checkIsRadioSelect.index(of: indexPath.row) //delete this line
let checkIndex = arr["default"] as! Int //use radio button key.
@IBAction func selectRadioButton(_ sender: KGRadioButton) {
let arr = detailsArray[sender.tag]
if sender.isSelected {
arr["default"] = @"0"
} else{
arr["default"] = @"1"
}
self.tableDetails.reloadData()
}