我上面的代码应该用我的模型症状填充 ios 列表:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) ->
UITableViewCell {
tableView.registerNib(UINib(nibName: "SymptomTableCell", bundle: nil),
forCellReuseIdentifier: "cell")
if let cell = tableView.dequeueReusableCellWithIdentifier("cell")
as? SymptomTableCell {
print(indexPath.row)
print(cell)
let symptom = symptoms[indexPath.row]
if Editor.isEditing {
cell.setDatasource(symptom, isSelected:
hasSymptom(symptomsEdit, oid: symptom.oid))
} else {
cell.setDatasource(symptom, isSelected: cell.swItem.on)
}
cell.tag = Helper.random()
cell.backgroundColor = UIColor.clearColor()
cell.selectionStyle = UITableViewCellSelectionStyle.None
tableCells.append(cell)
return cell
}
return UITableViewCell()
}
这两个是我实现 UITableView 的对象你可以看到附加的图像来理解错误:
import Foundation
import EVReflection
class Symptom: EVObject {
var oid = ""
var name = ""
}
import UIKit
class SymptomTableCell: UITableViewCell {
var symptom: Symptom?
@IBOutlet weak var swItem: UISwitch!
@IBOutlet weak var lblItem: UILabel!
func setDatasource(symptom: Symptom, isSelected: Bool) {
self.symptom = symptom
lblItem.text = symptom.name
swItem.on = isSelected
}
}
最佳答案
一些事情。首先,正如 rmaddy 在评论中提到的那样,删除:
tableView.registerNib(UINib(nibName: "SymptomTableCell", bundle: nil),
forCellReuseIdentifier: "cell")
来自cellForRowAtIndexPath
,需要进入viewDidLoad()
。
其次,在您的自定义单元格类中,我将覆盖 prepareForReuse
并将开关设置为关闭状态,然后根据需要在您的 VC 中的 cellForRowAtIndexPath
中打开它们,就像您目前是:
class SymptomTableCell: UITableViewCell {
var symptom: Symptom?
@IBOutlet weak var swItem: UISwitch!
@IBOutlet weak var lblItem: UILabel!
override func prepareForReuse() {
self.swItem.setOn(false, animated: false)
}
func setDatasource(symptom: Symptom, isSelected: Bool) {
self.symptom = symptom
lblItem.text = symptom.name
swItem.on = isSelected
}
}
关于ios - 在重用单元格并设置 UISwitch 是否处于事件状态时,ios listview 将第一行和最后一行设置为事件状态,有什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40161724/