2017-01-16 102 views
0

如果我在city选项卡下选择一个表格行,并按Ok以突出显示该行,当我处于areaOne下时,即使将其更改为areaTwo buttoncity下的同一行表格仍保持突出显示。我如何防止这种情况发生?如何根据按钮选择行?

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

您能否提供关于areaOne和areaTwo的更多上下文。也许提供一个截图? – JoGoFo

+0

在你现在的逻辑中,我只看到'cell.backgroundColor = UIColor(white:1.0,alpha:0.5)',没有看到之前选择的单元格的重置'cell.backgroundColor'片段。 – nynohu

回答

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数据对象的数组。就像如下 -

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") 
} 

现在,您可以按照您的要求更新表格。顺便说一句,这是一个关于如何保持细胞选择的粗略想法。对于更重要的事情,您可以根据自己的需要进行定制。

希望对您有所帮助:)

+0

我更新了我的问题,希望它更清楚,我有城市和城镇的名单,我希望选定的城市依靠按下的按钮保持突出显示。 – leaner122

+0

检查更新。:) – Tuhin

相关问题