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)
}
どのようなareaOneとareaTwoであるかをさらに詳しく説明できますか?多分、スクリーンショットを提供しますか? – JoGoFo
あなたのロジックでは 'cell.backgroundColor = UIColor(white:1.0、alpha:0.5)'しか表示されません。前に選択したセルの 'cell.backgroundColor'をリセットするためのスニペットは表示されません。 – nynohu