2017-01-16 11 views
0

cityタブの表の行を選択し、を押して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

どのようなareaOneとareaTwoであるかをさらに詳しく説明できますか?多分、スクリーンショットを提供しますか? – JoGoFo

+0

あなたのロジックでは 'cell.backgroundColor = UIColor(white:1.0、alpha:0.5)'しか表示されません。前に選択したセルの 'cell.backgroundColor'をリセットするためのスニペットは表示されません。 – nynohu

答えて

0

UITableviewCellの選択/選択解除後に常に発生する2つのデリゲートメソッドがあります。以下の方法を見てください。

// 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データを更新し、必要に応じてテーブル全体を更新できます。

EDIT

今、あなたの要件ごとに、あなたはの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

関連する問題