需要你的一点帮助,我正在研究 TableView ,其中我有一个从服务器接收的数组。该数组只不过是多项选择选项。看起来像这样
a.表
b.瓶子
c.帽子
d.桶
e.以上都不是。
我想要什么? 我想选择多个答案,即 Table 、 Bottle 、 Bucket ,它由标记的复选框显示。我已经设法做得很好了。当最后一个选项(即“以上均未选择”)时,我想取消选择上述所有选中标记选项,并仅显示选中的“无”选项,即使这是有效的。
我被困在哪里了? 当“以上都不是”处于选定模式并且我点击任何其他选项时,应取消选择“无”。这不起作用,我不知道这里出了什么问题。请帮忙。 TIA
这是我的 didSelect 方法
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
{
if selectedString == "None of the above" {
selectedIndex = indexPath.row
self.isNoneSelected = true
for index in tableView.indexPathsForSelectedRows!
{
tableView.deselectRowAtIndexPath(index, animated: true)
}
}
我的“None”选项将始终位于最后一个位置,因此我猜即使是 array.lastobject 也会起作用
最佳答案
你把事情搞得太复杂了。如果您添加 UIImageView 来表示选定状态,您所需要的只是一个 bool 数组来存储选定状态,以及用于 tableView 的 reloadData() 。 直接为每一行调用 tableView.deselectRow() 并不是一个好主意
var selected = Array(repeating: false, count: 5)
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) {
//let cell = whatever
cell.checkMark.isHidden = !selected[indexPath.row]
//return cell
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
{
selected[indexPath.row] = !selected[indexPath.row]
if (indexPath.row == selected.count - 1 ) {//if 'None' is selected
for i in 0..<selected.count-2 {
selected[i] = false
}
} else { //all other rows will deselect the 'None' row
selected[selected.count - 1 ] = false
}
tableView.reloadData()
}
关于ios - 快速点击(选择)最后一个选项时取消选择所选选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41228435/