當我選擇一個表格行並點擊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