gpt4 book ai didi

ios - Swift 上 Tableview 单元格中的矩阵调查

转载 作者:行者123 更新时间:2023-11-30 12:00:08 27 4
gpt4 key购买 nike

我在表格 View 单元格中使用DLRadioButton。我的表中有 30 个单元格,每个单元格有 4 个 DLRadioButton。例如,如果我在第一个单元格中选择了第一个单选按钮,则自动选择 5.、9.、13.、17.、21.、25. 和 29. 单元格的第一个单选按钮。我该如何解决这个问题?

DLRadioButton:https://github.com/DavydLiu/DLRadioButton

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "ac",for:indexPath) as! AnswerChoicesTableCell

cell.tag = data[indexPath.row]["ac_id"].intValue;
let acID:Int = data[indexPath.row]["ac_id"].intValue;
cell.ACTitle.text = data[indexPath.row]["ac_question"].stringValue

cell.ACButton1.setTitle(data[indexPath.row]["ac_choice_1"].stringValue, for: .normal)
cell.ACButton1.titleLabel?.numberOfLines = 0
cell.ACButton1.titleLabel?.lineBreakMode = .byWordWrapping
cell.ACButton1.acID = acID
if(data[indexPath.row]["ac_choice_1"].stringValue == "-"){
cell.ACButton1.isHidden = true
for constraint in cell.ACButton1.constraints {
if constraint.identifier == "h" {
constraint.constant = 0
}
}
cell.ACButton1.layoutIfNeeded()
}


cell.ACButton2.setTitle(data[indexPath.row]["ac_choice_2"].stringValue, for: .normal)
cell.ACButton2.titleLabel?.numberOfLines = 0
cell.ACButton2.titleLabel?.lineBreakMode = .byWordWrapping
cell.ACButton2.acID = acID
if(data[indexPath.row]["ac_choice_2"].stringValue == "-"){
cell.ACButton2.isHidden = true
for constraint in cell.ACButton2.constraints {
if constraint.identifier == "h" {
constraint.constant = 0
}
}
cell.ACButton2.layoutIfNeeded()

}


cell.ACButton3.setTitle(data[indexPath.row]["ac_choice_3"].stringValue, for: .normal)
cell.ACButton3.titleLabel?.numberOfLines = 0
cell.ACButton3.titleLabel?.lineBreakMode = .byWordWrapping
cell.ACButton3.acID = acID
if(data[indexPath.row]["ac_choice_3"].stringValue == "-"){
cell.ACButton3.isHidden = true
for constraint in cell.ACButton3.constraints {
if constraint.identifier == "h" {
constraint.constant = 0
}
}
cell.ACButton3.layoutIfNeeded()
}

cell.ACButton4.setTitle(data[indexPath.row]["ac_choice_4"].stringValue, for: .normal)
cell.ACButton4.titleLabel?.numberOfLines = 0
cell.ACButton4.titleLabel?.lineBreakMode = .byWordWrapping
cell.ACButton4.acID = acID

if(data[indexPath.row]["ac_choice_4"].stringValue == "-"){
cell.ACButton4.isHidden = true
for constraint in cell.ACButton4.constraints {
if constraint.identifier == "h" {
constraint.constant = 0
}
}
cell.ACButton4.layoutIfNeeded()


}


cell.ACButton4.otherButtons = [cell.ACButton1,cell.ACButton3,cell.ACButton2]

return cell
}

tableview screenshot

最佳答案

我解决了这个问题。我必须添加 DLRadiobutton 的“indexPath”属性。

在 Pods 文件夹中添加 DLRadioButton.m 文件;

@property (nonatomic) IBInspectable NSInteger indexPath;

tableviewController;

  func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "ac",for:indexPath) as! AnswerChoicesTableCell




cell.tag = data[indexPath.row]["ac_id"].intValue;
let acID:Int = data[indexPath.row]["ac_id"].intValue;
cell.ACTitle.text = data[indexPath.row]["ac_question"].stringValue


cell.ACButton1.setTitle(data[indexPath.row]["ac_choice_1"].stringValue, for: .normal)
cell.ACButton1.titleLabel?.numberOfLines = 0
cell.ACButton1.titleLabel?.lineBreakMode = .byWordWrapping
cell.ACButton1.acID = acID
cell.ACButton1.indexPath = indexPath.row;
if(data[indexPath.row]["ac_choice_1"].stringValue == "-"){
cell.ACButton1.isHidden = true
for constraint in cell.ACButton1.constraints {
if constraint.identifier == "h" {
constraint.constant = 0
}
}
cell.ACButton1.layoutIfNeeded()
}


cell.ACButton2.setTitle(data[indexPath.row]["ac_choice_2"].stringValue, for: .normal)
cell.ACButton2.titleLabel?.numberOfLines = 0
cell.ACButton2.titleLabel?.lineBreakMode = .byWordWrapping
cell.ACButton2.acID = acID
cell.ACButton2.indexPath = indexPath.row
if(data[indexPath.row]["ac_choice_2"].stringValue == "-"){
cell.ACButton2.isHidden = true
for constraint in cell.ACButton2.constraints {
if constraint.identifier == "h" {
constraint.constant = 0
}
}
cell.ACButton2.layoutIfNeeded()

}


cell.ACButton3.setTitle(data[indexPath.row]["ac_choice_3"].stringValue, for: .normal)
cell.ACButton3.titleLabel?.numberOfLines = 0
cell.ACButton3.titleLabel?.lineBreakMode = .byWordWrapping
cell.ACButton3.acID = acID
cell.ACButton3.indexPath = indexPath.row
if(data[indexPath.row]["ac_choice_3"].stringValue == "-"){
cell.ACButton3.isHidden = true
for constraint in cell.ACButton3.constraints {
if constraint.identifier == "h" {
constraint.constant = 0
}
}
cell.ACButton3.layoutIfNeeded()
}

cell.ACButton4.setTitle(data[indexPath.row]["ac_choice_4"].stringValue, for: .normal)
cell.ACButton4.titleLabel?.numberOfLines = 0
cell.ACButton4.titleLabel?.lineBreakMode = .byWordWrapping
cell.ACButton4.acID = acID
cell.ACButton4.indexPath = indexPath.row;
if(data[indexPath.row]["ac_choice_4"].stringValue == "-"){
cell.ACButton4.isHidden = true
for constraint in cell.ACButton4.constraints {
if constraint.identifier == "h" {
constraint.constant = 0
}
}
cell.ACButton4.layoutIfNeeded()


}
cell.ACButton1.addTarget(self, action:#selector(AnswerChoiesViewController.ACButtonClick(_:)),for: .touchUpInside)

cell.ACButton2.addTarget(self, action:#selector(AnswerChoiesViewController.ACButtonClick(_:)),for: .touchUpInside)

cell.ACButton3.addTarget(self, action:#selector(AnswerChoiesViewController.ACButtonClick(_:)),for: .touchUpInside)

cell.ACButton4.addTarget(self, action:#selector(AnswerChoiesViewController.ACButtonClick(_:)),for: .touchUpInside)

cell.ACButton1.otherButtons = [cell.ACButton4,cell.ACButton3,cell.ACButton2]
let acString = data[indexPath.row]["ac_id"].stringValue;
print(selectedIndex)
if(selectedIndex.contains((indexPath.row))){

//print(indexPath.row)
if(selectedChoices[acString] == 1){

cell.ACButton1.isSelected = true;

}
else if(selectedChoices[acString] == 2){
cell.ACButton2.isSelected = true;
}
else if(selectedChoices[acString] == 3){
cell.ACButton3.isSelected = true;
}
else if(selectedChoices[acString] == 4){
cell.ACButton4.isSelected = true;

}

}else{
cell.ACButton1.isSelected = false;
cell.ACButton2.isSelected = false;
cell.ACButton3.isSelected = false;
cell.ACButton4.isSelected = false;
}


return cell
}







@IBAction func ACButtonClick(_ sender:DLRadioButton) {
let acID = sender.acID as NSNumber
self.selectedChoices[acID.stringValue] = sender.tag
if(!self.selectedIndex.contains((sender.indexPath))){
self.selectedIndex.append(sender.indexPath)
}

}

关于ios - Swift 上 Tableview 单元格中的矩阵调查,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47349717/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com