Ios 如何在swift 3的表格视图中将仅选定行和未选定行的字体颜色和大小更改为正常?

Ios 如何在swift 3的表格视图中将仅选定行和未选定行的字体颜色和大小更改为正常?,ios,uitableview,swift3,Ios,Uitableview,Swift3,这里我需要使选中的行文本增加字体,更改颜色并使下划线类似于属性文本,我已完成此操作,但这里我只需要使选定的单元格文本像上述属性一样高亮显示,当我单击另一行时,我需要使其余单元格文本正常,而不增加字体大小,着色并使下划线和剩余单元格保持正常。有人能帮我如何实现这一点吗 使表示当前选定行索引的整数值,并在CellForRow中执行此操作 if(index != -1 ) { if(index == indexPath.row) {

这里我需要使选中的行文本增加字体,更改颜色并使下划线类似于属性文本,我已完成此操作,但这里我只需要使选定的单元格文本像上述属性一样高亮显示,当我单击另一行时,我需要使其余单元格文本正常,而不增加字体大小,着色并使下划线和剩余单元格保持正常。有人能帮我如何实现这一点吗


使表示当前选定行索引的整数值,并在CellForRow中执行此操作

  if(index != -1 )
    {
        if(index == indexPath.row)
        {

            cell.leftLb.font = UIFont.init(name: "Helvetica", size: 50)

       }
       else
       {
          cell.leftLb.font = UIFont.init(name: "Helvetica", size: 10)

        }

    }
当didSelectRow单击更新索引并重新加载表时

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
{


    index = indexPath.row

    self.areaSettTable.reloadData()


}

使用此代码:

设置选择颜色

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
{
  let selectedCell:UITableViewCell = tableView.cellForRow(at: indexPath as IndexPath)!
  selectedCell.categoriesProductsLabel.font = UIFont(name:"Bold", size:17)
  selectedCell.contentView.backgroundColor = UIColor.green
}
func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath){
    let cell:UITableViewCell = tableView.cellForRow(at: indexPath as IndexPath)!
    cell.categoriesProductsLabel.font = UIFont(name:"*Set whatever you want here*", size:14)
    cell.contentView.backgroundColor = UIColor.black
}
设置选择颜色

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
{
  let selectedCell:UITableViewCell = tableView.cellForRow(at: indexPath as IndexPath)!
  selectedCell.categoriesProductsLabel.font = UIFont(name:"Bold", size:17)
  selectedCell.contentView.backgroundColor = UIColor.green
}
func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath){
    let cell:UITableViewCell = tableView.cellForRow(at: indexPath as IndexPath)!
    cell.categoriesProductsLabel.font = UIFont(name:"*Set whatever you want here*", size:14)
    cell.contentView.backgroundColor = UIColor.black
}

使用-1初始化保存当前选择的变量,然后在选择单元格时将行数设置为其值。仅重新加载上一个选择和当前选择单元格。无需重新加载整个tableview

let arrData:[String] = ["Woman's Clothing","Men's Clothing","Bags","Beauty","Sports","Shoes"]
var currentSelection = -1
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    return self.arrData.count

}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "cell")
    cell?.textLabel?.text = self.arrData[indexPath.row]
    if indexPath.row == currentSelection {

        cell?.textLabel?.textColor = UIColor.blue //Apply font, color for selected cell
    }else {

        cell?.textLabel?.textColor = UIColor.black //Apply font, color for non selected cell
    }
    return cell!
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    let previousSelection = self.currentSelection
    self.currentSelection = indexPath.row
    if previousSelection == -1 {

        self.tableView.reloadRows(at: [indexPath], with: .none)

    }else {

        let prevIndexPath = IndexPath(row: previousSelection, section: 0)
        self.tableView.reloadRows(at: [prevIndexPath,indexPath], with: .none)
    }
}

尝试使用相同的代码,并进行以下更改:

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

  if indexPath.row == currentSelection {

  let item = categoriesModelClass?.childrenData[indexPath.row]
  cell.categoriesProductsLabel.font = UIFont(name:"Bold", size:17)
  cell.categoriesProductsLabel.textColor = UIColor.black
  let underlineAttribute = [NSUnderlineStyleAttributeName: NSUnderlineStyle.styleSingle.rawValue]
  let underlineAttributedString = NSMutableAttributedString(string: (item?.name)! , attributes: underlineAttribute)
  underlineAttributedString.addAttribute(NSUnderlineColorAttributeName, value: UIColor.cyan, range: NSRange(location: 0, length: (item?.name?.characters.count)!))
  cell.categoriesProductsLabel.attributedText = underlineAttributedString

  }else {

   let item = categoriesModelClass?.childrenData[indexPath.row]
   cell.categoriesProductsLabel.textColor = UIColor.darkGray
   cell.categoriesProductsLabel.font = UIFont(name:"Bold", size:10)
   cell.categoriesProductsLabel.text = item?.name

}
    return cell
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    self.currentSelection = indexPath.row
    self.sideTableView.reloadData()

}

然后,此代码应放置在索引路径行的单元格中,以便在重新加载期间处理。我需要在选择一行后,它需要高亮显示,如图所示。但是,当我在另一行上选择时,我需要高亮显示此行,并且以前选择的行需要正常显示。是,此代码将正常工作。请检查更新的代码。您可以根据自己的选择更改颜色。此逻辑的目的是什么?但在特定情况下它失败@PriyaI无法通过查看图像获得问题。你能详细解释一下这个问题吗?当我一个接一个地单击时,它工作得很好,但当我连续单击时,字体大小没有变为正常大小,请看我的图像@Priyahere我编辑了我的代码,请检查它是否在连续单击时失败,就像在选择第一行后单击第三行或第四行,然后失败@PriyaNo需要在重新加载单元格时更改
didSelectRowAt
中的字体,这将自动影响如果我选择了一个表格视图单元格,则所选标签与我发布的代码配合良好,但当我更改选择单元格的方向时,则它无法正常工作@Shezad