如何在表格视图中增加所选索引的字体颜色和字体大小?

问题描述 投票:0回答:3

在这里,我需要实现此页面,如图所示,为此我放置了一个表视图和集合视图,但是如果我选择特定的类别,那么如何使选定的字体大小更大,字体颜色需要如图所示进行更改在图像,但运行应用程序后,如果我选择屏幕打开当时第一次默认它应该被选择是What's New后来它需要更改为我选择的任何人可以帮助我如何解决这个问题?

这是我的屏幕,设计如下:

enter image description here

这是我设计的屏幕,其中包含以下问题:

enter image description here

这是我已经尝试过的代码

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)
    }
}
ios uitableview swift3
3个回答
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)

     }
}

0
投票

覆盖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)

}    

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)
© www.soinside.com 2019 - 2024. All rights reserved.