如果我在city
标签下选择一个表格行,并在我Ok
下方时按areaOne
突出显示该行,city
标签下的同一表格行仍会突出显示如果我将其更改为areaTwo button
。我该如何防止这种情况发生?
var citySelectedIndexPaths = [IndexPath]()
var townSelectedIndexPaths = [IndexPath]()
@IBOutlet weak var areaOne: UIButton!
@IBOutlet weak var areaTwo: UIButton!
var popValue:Int!
@IBAction func areaSelect(_ sender: UIButton) {
let areas: [[UIButton]] = [[areaOne],[areaTwo]]
switch sender {
case areaOne:
popValue = 1
case areaTwo:
popValue = 2
default: break
}
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
let myCell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! CityCell
//resetting the color
myCell.backgroundColor = UIColor(white: 1.0, alpha:1.0)
switch (segCtrl.selectedSegmentIndex)
{
case 0:
myCell.myLabel.text = cityName[indexPath.row]
//I like to know how to add condition for button selected below
// so as to display popValue =1 when areaOne is tapped
//instead of me doing it manually like below
if citySelectedIndexPaths.contains(indexPath) && (popValue == 1) {
myCell.backgroundColor = UIColor(white: 1.0, alpha:0.5)
}
break
case 1:
myCell.myLabel.text = townName[indexPath.row]
if townSelectedIndexPaths.contains(indexPath) {
myCell.backgroundColor = UIColor(white: 1.0, alpha:0.5)
}
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)
switch (segCtrl.selectedSegmentIndex)
{
case 0:
if citySelectedIndexPaths.contains(indexPath) {
let indexValue = citySelectedIndexPaths.indexOf(indexPath)
citySelectedIndexPaths.removeAtIndex(indexValue)
} else {
citySelectedIndexPaths.append(indexPath)
}
break
case 1:
if townSelectedIndexPaths.contains(indexPath) {
let indexValue = townSelectedIndexPaths.indexOf(indexPath)
townSelectedIndexPaths.removeAtIndex(indexValue)
} else {
townSelectedIndexPaths.append(indexPath)
}
break
default:break
}
})
alertController.addAction(ok)
present(alertController, animated: true, completion: nil)
}
答案 0 :(得分:0)
在选择/取消选择UITableviewCell
之后,总会触发两个委托方法。请看下面的方法。
// Called after the user changes the selection.
@available(iOS 2.0, *)
optional public func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) //Called after selection of cell
@available(iOS 3.0, *)
optional public func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) //Called after deselection of cell.
现在你的观点是根据按钮选择进行更改 -
func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
let deSelectedCell = tableView.cellForRow(at: indexPath) as? UITableviewCell
deSelectedCell?.toggleCheckBox()
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let selectedCell = tableView.cellForRow(at: indexPath) as? UITableviewCell
selectedCell?.toggleCheckBox()
}
在我的情况下,toggleCheckBox()
是一个函数,它根据单元格内部的单元格选择/取消选择来确定复选框的选择/取消选择。
不要更新cellForRowAt indexPath
内的单元格选择,而是尝试在这些委托中更新。虽然通过查看您的问题但您正在尝试处理的情况尚不清楚,但您可以根据选择和取消选择更新此委托的主tableView数据,并根据您的需要更新整个表。
修改强>
现在根据您的要求,您可以为tableView数据创建一个对象数组。如下所示 -
private var orderDetailsTableViewData: [[String: Any]] = [["cellSelectionColor": UIColor.blue], ["cellSelectionColor": UIColor.white]]
您只需根据选择更新代码,并根据要求更新tableView。
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cellIdentifier", for: indexPath) as! UITableviewCell()
if let cellColor = orderDetailsTableViewData[indexPath.row]["cellSelectionColor"] as? UIColor{
cell.backgroundColor = cellColor
}else{
cell.backgroundColor = UIColor.clear
}
return cell
}
func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
orderDetailsTableViewData[indexPath.row].updateValue(UIColor.blue, forKey: "cellSelectionColor")
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
orderDetailsTableViewData[indexPath.row].updateValue(UIColor.white, forKey: "cellSelectionColor")
}
此后,您可以根据自己的要求更新表格。顺便说一句,这是一个关于如何保持细胞选择的粗略想法。对于更重要的事情,您可以根据需要进行自定义。
希望这有帮助。:)