【发布时间】:2015-10-19 05:45:23
【问题描述】:
我有一个函数可以将 Section 0 中单元格的 accessoryType 更改为复选标记。它工作得很好,并在选中时显示一个复选标记,但如果选择了 Section 1 或 Section 2 中的单元格,它将移动 Section 0。如果有人能告诉我如何解决这个问题,那就太好了。
//Adds Checkmark to Cells when Selected
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let section = 0
let numberOfRows = tableView.numberOfRowsInSection(section)
for row in 0..<numberOfRows {
if let cell = tableView.cellForRowAtIndexPath(NSIndexPath(forRow: row, inSection: section)) {
cell.accessoryType = row == indexPath.row ? .Checkmark : .None
}
}
}
【问题讨论】:
-
您是否启用了多项选择?我假设您一次只能选择一行,这就是为什么您在选择其他内容时会松开复选标记
-
另外,为什么不在 cellForRowatIndexPath 出列单元格时添加属性类型?
-
是的,我有三个部分。如果我按下第二部分的第一个单元格,它将选中第一部分的第一个单元格。每个部分的第二个和第三个单元格也是如此。我只想在第一部分使用此代码。
标签: swift uitableview cells didselectrowatindexpath sections