2017-01-10 65 views
0

當我選擇一個表格行並點擊city標籤下的'Ok'時,它突出顯示了行,但是當我將標籤更改爲town時,它突出顯示town標籤中的同一行,即使我沒有選擇它。也就是說,我突出顯示city選項卡下的第二和第三行,它突出顯示第town選項卡下的第二和第三行,而無需執行任何操作。下面是我的代碼如何防止在下一段中突出顯示錶格行?

public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{ 
    let myCell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! CityCell 
    switch (segCtrl.selectedSegmentIndex) 
    { 
    case 0: 
     myCell.myLabel.text = cityName[indexPath.row] 
     break 
    case 1: 
     myCell.myLabel.text = townName[indexPath.row] 
     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) 
    }) 
let cancel = UIAlertAction(title: "Cancel", style: .cancel, handler: { (action) -> Void in 
}) 
    alertController.addAction(ok) 
    alertController.addAction(cancel) 
present(alertController, animated: true, completion: nil) 
} 

如何防止這種高亮從下一個標籤發生,它限制的地方,我居然選擇了行?

回答

1

我在使用objective-c。所以,如果我的swift代碼錯誤,很抱歉。

// global variable which carry selection-indexPath 
var citySelectedIndexPaths = [IndexPath]() 
var townSelectedIndexPaths = [IndexPath]() 

public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{ 
    let myCell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! CityCell 

    // add this line 
    myCell.backgroundColor = UIColor(white: 1.0, alpha:1.0) // default color 

    switch (segCtrl.selectedSegmentIndex) 
    { 
    case 0: 
     myCell.myLabel.text = cityName[indexPath.row] 
     if citySelectedIndexPaths.contains(indexPath) { 
      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) 

    // save selection indexPath 
    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 
    } 

    }) 
let cancel = UIAlertAction(title: "Cancel", style: .cancel, handler: { (action) -> Void in 
}) 
    alertController.addAction(ok) 
    alertController.addAction(cancel) 
present(alertController, animated: true, completion: nil) 
} 
+0

當我回來時,這並不保留選擇。 – leaner122

+0

是的,當您更改選項卡時,它只是將單元格重置爲正常。如果你想保持選擇,當你點擊'OK'時,你應該將選擇的indexPath.row保存在數組或全局變量中。然後使用if語句來決定單元格應該在switch語句中突出顯示。 – Hezron

+0

這是工作,但它只記住所選的最後一行。這是如果我選擇三行,它只突出顯示最後選中的行,其餘兩行未選中。 – leaner122

0

現在在您的代碼中,您可以手動設置背景。

cell.backgroundColor = UIColor(white: 1.0, alpha:0.5) 

但是您在選擇其他單元格後沒有重置cell.backgroundColor。因此,您需要存儲當前選定的cell's indexPath,並且每次在ok UIAlertAction中選擇新的單元時請致電reloadData。在cellForRowAtIndexPath中,只需執行具有選定或未選定階段的單元格。