我有几个带有UISwitch的单元格作为附件视图,只要数据正确就可以了,我可以切换它们。我遇到的问题是,当我切换一个然后滚动时,另一个切换了,而当我向后滚动时,切换的那个不再打开。我假设这与要重新加载的单元格有关。
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
switch indexPath.row {
case 0, 1, 3, 4, 6, 8, 10, 12, 14, 16:
var cell = tableView.dequeueReusableCell(withIdentifier: "Cell")
if cell == nil
{
cell = UITableViewCell(style: UITableViewCell.CellStyle.default, reuseIdentifier: "Cell")
}
cell?.selectionStyle = .none
cell?.textLabel?.text = questions[indexPath.row]
cell?.textLabel?.numberOfLines = 0
let answerSwitch = UISwitch(frame: .zero)
answerSwitch.isOn = false
answerSwitch.addTarget(self, action: #selector(switchToggled(sender:)), for: .valueChanged)
answerSwitch.tag = indexPath.row
cell?.accessoryView = answerSwitch
return cell!
case 2, 5, 7, 9, 11, 13, 15, 17:
let cell = tableView.dequeueReusableCell(withIdentifier: "QuestionDetailCell", for: indexPath) as! RAProfileQuestionDetailCell
cell.selectionStyle = .none
cell.questionLabel.text = questions[indexPath.row]
cell.answerField.inputAccessoryView = self.keyboardToolbar
cell.answerField.tag = indexPath.row
return cell
default:
let cell = tableView.dequeueReusableCell(withIdentifier: "QuestionCell", for: indexPath) as! RAProfileQuestionCell
return cell
}
}
开关切换:
@objc func switchToggled(sender: Any) {
let answerSwitch = sender as! UISwitch
print(answerSwitch.tag)
switch answerSwitch.tag {
case 0:
if answerSwitch.isOn {
self.pre_existing = true
} else {
self.pre_existing = false
}
case 1 :
if answerSwitch.isOn {
self.take_medication = true
} else {
self.take_medication = false
}
case 3:
if answerSwitch.isOn {
self.suicide = true
} else {
self.suicide = false
}
case 4:
if answerSwitch.isOn {
self.previous_treatment = true
} else {
self.previous_treatment = false
}
case 6:
if answerSwitch.isOn {
self.surgery = true
} else {
self.surgery = false
}
case 8:
if answerSwitch.isOn {
self.family_substance_abuse = true
} else {
self.family_substance_abuse = false
}
case 10:
if answerSwitch.isOn {
self.allergies = true
} else {
self.allergies = false
}
case 12:
if answerSwitch.isOn {
self.other_drug_use = true
} else {
self.other_drug_use = false
}
case 14:
if answerSwitch.isOn {
self.alcohol = true
} else {
self.alcohol = false
}
case 16:
if answerSwitch.isOn {
self.tobacco = true
} else {
self.tobacco = false
}
default:
break
}
}
答案 0 :(得分:1)
表单元格被重用,此外,您还可以使用默认的false
创建另一个1
let answerSwitch = UISwitch(frame: .zero)
answerSwitch.isOn = false
answerSwitch.addTarget(self, action: #selector(switchToggled(sender:)), for: .valueChanged)
answerSwitch.tag = indexPath.row
cell?.accessoryView = answerSwitch
但是您需要
answerSwitch.isOn = // set old value
您应该根据switchToggled
在cellForRowAt
中进行与indexPath.row
中相同的切换,或者创建一个模型数组,但是您的问题是单元格杂乱,因此数组实现会一点都不容易
答案 1 :(得分:0)
乍看之下,您的cellForRowAtIndexPath answerSwitch.isOn = false
告诉开关每次单元格进入屏幕时都要关闭。由于您看起来像是根据开关是打开还是关闭来存储一堆布尔值,因此应使用这些变量来告知开关应处于什么状态。
看起来应该更像answerSwitch.isOn = self.tobacco
(或者它应该在那个indexPath的哪个属性)。这样做可以保持开关状态。