2017-01-16 63 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