如何在swift 3的表格视图中仅更改选定行和未选定行的字体颜色和大小?
Posted
技术标签:
【中文标题】如何在swift 3的表格视图中仅更改选定行和未选定行的字体颜色和大小?【英文标题】:How to change font colour and size for only selected row and for unselected rows to be normal in table view in swift 3? 【发布时间】:2017-12-26 09:21:58 【问题描述】:在这里我需要制作选定的行文本以增加字体、更改颜色并像属性文本一样制作下划线,我已经完成了但在这里我只需要像上面告诉的属性一样突出显示选定的单元格文本,当我点击在另一行上,我需要在不增加字体大小、颜色的情况下使剩余的单元格文本正常,并使下划线和剩余的单元格正常,谁能帮我实现这个?
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.text = item?.name
return cell
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
let previousSelection = self.currentSelection
self.currentSelection = indexPath.row
if previousSelection == -1
let index = indexPath
let cell = self.sideTableView.cellForRow(at: indexPath) as! categoriesTableViewCell
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
sideTableView.reloadRows(at: [indexPath], with: .none)
else
let prevIndexPath = IndexPath(row: previousSelection, section: 0)
self.sideTableView.reloadRows(at: [prevIndexPath,indexPath], with: .none)
【问题讨论】:
无需更改didSelectRowAt
中的字体,因为您重新加载单元格会自动影响
【参考方案1】:
使用此代码:
设置选择颜色
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
【讨论】:
我需要在选择一行后突出显示,如图所示,但是当我在另一行上选择时,我需要突出显示这一行并且之前选择的行需要正常 是的,此代码将起作用。请检查更新后的代码。您可以根据自己的选择更改颜色。【参考方案2】:制作代表当前选定行索引的整数值并在 cellForRowAt 中执行此操作
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()
【讨论】:
那么这段代码应该放在单元格中,以便在重新加载期间处理索引路径处的行【参考方案3】:尝试使用相同的代码进行以下更改:
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()
【讨论】:
如果我选择一个表格视图单元格,那么所选标签与我发布的代码一起工作正常,但是当我改变选择单元格的方向时,它就不能正常工作@Shezad【参考方案4】:使用 -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)
【讨论】:
这个逻辑的目的是什么? 但在特定情况下会失败@Priya 我无法通过查看图像来解决问题。你能详细说明一下这个问题吗? 当我一个一个点击时它工作得很好,但是当我连续点击时字体大小没有改变到正常,只是在我的图片中看到@Priya 在这里我编辑了我的代码,只是检查它是否失败,当你连续点击时,就像选择第一行后点击第三行或第四行然后它失败@Priya以上是关于如何在swift 3的表格视图中仅更改选定行和未选定行的字体颜色和大小?的主要内容,如果未能解决你的问题,请参考以下文章