这里我需要实现此页面,如图所示,我放置了一个表格视图和集合视图,但是如果我选择特定的类别,那么如何使选定的字体大小更大,字体颜色需要是如图所示更改,但在运行应用程序后,如果我选择要在此时打开的屏幕,则应首先选择What's New
,然后需要更改为我选择的内容,任何人都可以帮我解决此问题?
这是我设计的屏幕:
以下是我设计的屏幕及其中的问题:
这是我已经尝试过的代码
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]
print(item?.name)
cell.categoriesProductsLabel.textColor = UIColor.darkGray
//cell.categoriesProductsLabel.font = UIFont.init(name: "Regular", size: 12)
cell.categoriesProductsLabel.text = item?.name
}
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let previousSelection = self.currentSelection
self.currentSelection = indexPath.row
self.categoriesCollectionView.reloadData()
print(indexPath.row)
if previousSelection == 0 {
sideTableView.reloadRows(at: [indexPath], with: .none)
}
else {
let prevIndexPath = IndexPath(row: previousSelection, section: 0)
self.sideTableView.reloadRows(at: [prevIndexPath,indexPath], with: .none)
}
}
答案 0 :(得分:0)
覆盖 categoriesTableViewCell 类中的setSelected方法,并且不要在 cellForRowAt 或 didSelectRowAt
中进行更新override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
if selected{
categoriesProductsLabel.font = UIFont(name:"Bold", size:17)
categoriesProductsLabel.textColor = UIColor.black
}else{
categoriesProductsLabel.textColor = UIColor.darkGray
categoriesProductsLabel.font = UIFont.init(name: "Regular", size: 12)
}
}
答案 1 :(得分:0)
Ovveride didSelectRowAtIndexPath和didDeselectRowAtIndexPath两种方法都可以更改和恢复原始字体大小和颜色
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
//get cell with indexpath
cell.categoriesProductsLabel.font = UIFont(name:"Bold", size:17)
cell.categoriesProductsLabel.textColor = UIColor.black
}
func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
//get cell with indexpath
cell.categoriesProductsLabel.textColor = UIColor.darkGray
cell.categoriesProductsLabel.font = UIFont.init(name: "Regular", size: 12)
}
答案 2 :(得分:0)
如果条件和代码也可以按你的意愿运行,你必须更新: -
替换这个: -
if previousSelection == 0 {
sideTableView.reloadRows(at: [indexPath], with: .none)
}
else {
let prevIndexPath = IndexPath(row: previousSelection, section: 0)
self.sideTableView.reloadRows(at: [prevIndexPath,indexPath], with: .none)
}
这一个: -
let prevIndexPath = IndexPath(row: previousSelection, section: 0)
self.sideTableView.reloadRows(at: [prevIndexPath,indexPath], with: .none)