2017-01-10 69 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中,只需执行具有选定或未选定阶段的单元格。