在这里,我需要实现此页面,如图所示,为此我放置了一个表视图和集合视图,但是如果我选择特定的类别,那么如何使选定的字体大小更大,字体颜色需要如图所示进行更改在图像,但运行应用程序后,如果我选择屏幕打开当时第一次默认它应该被选择是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)
}
}
覆盖您的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)
}
}
覆盖didSelectRowAtIndexPath和didselectRowAtIndexPath两种方法来更改和恢复原始字体大小和颜色
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)
}
你必须更新条件,你的代码也可以按你的意愿工作: -
替换这个: -
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)