UIButton在滚动多个部分的tableview时更改状态-Swift
我的tableview中有多个部分,其中包含多个自定义单元格(带有单选按钮的单元格、带有复选框的单元格和带有文本字段的单元格)。问题在单选按钮部分下,单选按钮部分下只能选择一个单选按钮。选择后,我尝试了滚动,选择了多个单选按钮。非常感谢你的帮助UIButton在滚动多个部分的tableview时更改状态-Swift,swift,uitableview,uibutton,Swift,Uitableview,Uibutton,我的tableview中有多个部分,其中包含多个自定义单元格(带有单选按钮的单元格、带有复选框的单元格和带有文本字段的单元格)。问题在单选按钮部分下,单选按钮部分下只能选择一个单选按钮。选择后,我尝试了滚动,选择了多个单选按钮。非常感谢你的帮助 class RedioButtonCell: UITableViewCell { var radioButtonDelegate: RedioCellDelegate? var cellindexPath : IndexPath? @IBOutle
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单元格被重用(在滚动时发生),因此您需要通过保存选定单元格的
indepath
并在cellForRowAt
//
在你的VC中声明这一点
var currentIndex:IndexPath?
//
//
//
tableView单元格被重用(在滚动时发生),因此您需要通过保存选定单元格的
indepath
并在cellForRowAt
//
在你的VC中声明这一点
var currentIndex:IndexPath?
//
//
//
您可以提供一些示例吗?单选按钮行在节下。您也可以在上面的回答中使用标记属性代替indexpath。@shubham它需要从字符串中再次处理行和节,是的,这是另一种选择。您可以将其存储在全局变量中,也可以在为按钮设置标记时使用变量。那么就不需要额外的处理了。您可以提供一些示例吗?单选按钮行位于节下。您也可以在上面的回答中使用标记属性代替indexpath。@shubham它需要从字符串中再次处理行和节,是的,这是另一种选择。您可以将其存储在全局变量中,也可以在为按钮设置标记时使用变量。这样就不会发生额外的处理
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
}