如果我在city
選項卡下選擇一個表格行,並按Ok
以突出顯示該行,當我處於areaOne
下時,即使將其更改爲areaTwo button
,city
下的同一行表格仍保持突出顯示。我如何防止這種情況發生?如何根據按鈕選擇行?
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