UISwitch TableViewCell
UISwitch TableViewCell
我有几个带有 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
}
}
switchToggled:
@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
}
}
Table 单元格被重复使用,此外,您在此处创建另一个 1,默认值为 false
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
根据 indexPath.row
,你在 switchToggled
中所做的相同切换应该在 cellForRowAt
中完成,或者创建一个模型数组,但你的问题是单元格混乱,所以数组实施起来有点不容易
乍一看,在您的 cellForRowAtIndexPath 中,answerSwitch.isOn = false
告诉开关在每次单元格出现在屏幕上时关闭。由于看起来您根据开关是打开还是关闭存储了一堆布尔值,因此您应该使用这些变量来告诉开关它应该处于什么状态。
它应该看起来更像 answerSwitch.isOn = self.tobacco
(或者 属性 它应该在那个 indexPath 中)。这样做应该保持开关状态。
我有几个带有 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
}
}
switchToggled:
@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
}
}
Table 单元格被重复使用,此外,您在此处创建另一个 1,默认值为 false
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
根据 indexPath.row
,你在 switchToggled
中所做的相同切换应该在 cellForRowAt
中完成,或者创建一个模型数组,但你的问题是单元格混乱,所以数组实施起来有点不容易
乍一看,在您的 cellForRowAtIndexPath 中,answerSwitch.isOn = false
告诉开关在每次单元格出现在屏幕上时关闭。由于看起来您根据开关是打开还是关闭存储了一堆布尔值,因此您应该使用这些变量来告诉开关它应该处于什么状态。
它应该看起来更像 answerSwitch.isOn = self.tobacco
(或者 属性 它应该在那个 indexPath 中)。这样做应该保持开关状态。