当我选择一个表格行并点击city
标签下的'Ok'
时,它突出显示了行,但是当我将标签更改为town
时,它突出显示town
标签中的同一行,即使我没有选择它。也就是说,我突出显示city
选项卡下的第二和第三行,它突出显示第town
选项卡下的第二和第三行,而无需执行任何操作。下面是我的代码如何防止在下一段中突出显示表格行?
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
let myCell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! CityCell
switch (segCtrl.selectedSegmentIndex)
{
case 0:
myCell.myLabel.text = cityName[indexPath.row]
break
case 1:
myCell.myLabel.text = townName[indexPath.row]
break
default:break
}
return myCell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){
let cell = tableView.cellForRow(at: indexPath)!
let alertController = UIAlertController(title: nil, message: nil, preferredStyle: .alert)
let ok = UIAlertAction(title: "Ok", style: .default, handler: { (action) -> Void in
let cell = tableView.cellForRow(at: indexPath)!
cell.backgroundColor = UIColor(white: 1.0, alpha:0.5)
})
let cancel = UIAlertAction(title: "Cancel", style: .cancel, handler: { (action) -> Void in
})
alertController.addAction(ok)
alertController.addAction(cancel)
present(alertController, animated: true, completion: nil)
}
如何防止这种高亮从下一个标签发生,它限制的地方,我居然选择了行?
当我回来时,这并不保留选择。 – leaner122
是的,当您更改选项卡时,它只是将单元格重置为正常。如果你想保持选择,当你点击'OK'时,你应该将选择的indexPath.row保存在数组或全局变量中。然后使用if语句来决定单元格应该在switch语句中突出显示。 – Hezron
这是工作,但它只记住所选的最后一行。这是如果我选择三行,它只突出显示最后选中的行,其余两行未选中。 – leaner122