【发布时间】:2018-12-12 13:33:49
【问题描述】:
我有模型数据,它有一个布尔值,表示是否应显示复选标记(accessoryType 为 .checkmark)...
例如,我希望在开始时选中五行中的两行(根据我所说的模型)...问题是,我可以显示复选标记,但是在我点击它们之后,切换不会不能正常工作:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if let cell = tableView.cellForRow(at: indexPath) {
cell.accessoryType = .checkmark
model[indexPath.row].isCellSelected = true
}
}
func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
if let cell = tableView.cellForRow(at: indexPath) {
cell.accessoryType = .none
model[indexPath.row].isCellSelected = false
}
}
And here is a cellForRowAt:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let data = model[indexPath.row]
let identifier = data.subtitle != nil ? kSubtitleID : kNormalID
let cell = tableView.dequeueReusableCell(withIdentifier: identifier, for: indexPath)
cell.textLabel?.text = data.title
cell.detailTextLabel?.text = data.subtitle
return cell
}
我可以这样显示复选标记:
cell.accessoryType = data.isCellSelected ? .checkmark : .none
但是当我点击它时,它会导致它被选中(allowsMultipleSelection 设置为 true),它不会被切换,而是第一次停留。
这是我使用的模型。真的很简单:
struct CellModel{
var title:String?
var subtitle:String?
var isCellSelected:Bool = false
}
【问题讨论】:
-
只显示您使用的模型结构,可以通过
cellForRowAt和didSelectRowAt..完成。 -
@vaibhav 我添加了模型示例。
-
如果你还是卡住了,我把我的答案发过来看看。
标签: ios swift uitableview tableview accessorytype