如何防止下一段表格行突出显示?

时间:2017-01-10 08:25:16

标签: ios swift uitableview uisegmentedcontrol

当我选择一个表格行并点击'Ok'标签下的city时,它会突出显示该行,但当我将标签更改为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)
}

如何防止此突出显示在下一个标签中发生,并将其限制在我实际选择行的位置?

2 个答案:

答案 0 :(得分:1)

我正在使用objective-c。所以,对不起,如果我的快速代码是错误的。

// 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)
}

答案 1 :(得分:0)

现在,在您的代码中,您可以手动设置背景。

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

但是在选择其他单元格后,您没有重置cell.backgroundColor。因此,每次在cell's indexPath中选择新单元格时,您都需要存储当前选定的reloadData并调用ok UIAlertAction。在cellForRowAtIndexPath中,只需执行选定或未选择阶段的单元格。