【问题标题】:When i try to apply "Font" to one tableview cell it automatically apply to other cells when scroll tableview当我尝试将“字体”应用于一个表格视图单元格时,它会在滚动表格视图时自动应用于其他单元格
【发布时间】:2015-09-19 13:10:45
【问题描述】:

我有一个自定义表格视图。

我需要将Bold Italic(Helvetica-BoldOblique)字体设置为仅单元格。但是当它滚动tableview时,它也会一一应用于其他单元格。如何解决这个问题?

    func applyFontToTableviewCell() {

    var couIn = NSIndexPath(forRow: 2, inSection: 0)
    var couCell = colorTableView.cellForRowAtIndexPath(couIn)
    couCell?.textLabel?.font = UIFont(name: "Helvetica-BoldOblique", size: 18.0)


    }

我也在 cellForRowAtIndexPath 中尝试了相同的代码。但是发生了同样的问题。

提前致谢。

【问题讨论】:

  • if (NSIndexPath(forRow: 0, inSection: 0)==1){ couCell?.textLabel?.font = UIFont(name: "Helvetica-BoldOblique", size: 18.0)} else{ //使用普通字体 }
  • 我知道是什么问题...
  • 只有一排你需要加粗休息是正常的,对吗?
  • 感谢您的回复,您能解释一下是什么问题
  • 不仅一行可以扩展到一两行,但不是所有行。

标签: swift uitableview fonts uifont custom-cell


【解决方案1】:

我有同样的问题,并通过在willDisplayCell 方法中应用字体来解决

override func tableView(_ tableView: UITableView, willDisplay cell:  UITableViewCell, forRowAt indexPath: IndexPath) {
        let row = indexPath.row

        if row == 2 || row == 4 || row == 6 {
          cell.textLabel?.font = UIFont(name: "Helvetica-BoldOblique", size: 18.0)
       }
      else{
           cell.textLabel?.font = UIFont(name: "Helvetica", size: 18.0)
       }
    }

【讨论】:

    【解决方案2】:

    您可以像下面这样在替代单元格上应用粗体字体。

     func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
                let couCell = tableView.dequeueReusableCellWithIdentifier(textCellIdentifier, forIndexPath: indexPath) as UITableViewCell
    
                let row = indexPath.row
    
                if row == 2 || row == 4 || row == 6 {
                  couCell?.textLabel?.font = UIFont(name: "Helvetica-BoldOblique", size: 18.0)
               }
              else{
                   couCell?.textLabel?.font = UIFont(name: "Helvetica", size: 18.0)
               }
    
                return cell
            }
    

    希望对您有所帮助。

    【讨论】:

    • 谢谢。但是有同样的问题
    • @Lyida,你遇到了什么问题?
    • 意味着,您只需要在 2,4,6 单元格上加粗文本吗?其他都是正常字体。对吗?
    • 是的。但是当滚动表格视图时,所有单元格文本都是粗体
    • @Lyida,我已根据您的要求更新了代码。请更换它。它在我的最后工作正常。
    【解决方案3】:

    你必须检查 if 条件:

    if (NSIndexPath(forRow: 2, inSection: 0)){
          couCell?.textLabel?.font = UIFont(name: "Helvetica-BoldOblique", size: 18.0)
    }
    else{
          //set your default font
          couCell?.textLabel?.font = UIFont(name: "Helvetica", size: 18.0)
    }
    

    如果你想检查奇数和偶数,那么

    if yourindexpath % 2 == 0 {
    }
    

    所以,可能是这样的

    if (NSIndexPath(forRow: indexPath.row % 2, inSection: 0)){
      couCell?.textLabel?.font = UIFont(name: "Helvetica-BoldOblique", size: 18.0)
    }
    else{
      //set your default font
      couCell?.textLabel?.font = UIFont(name: "Helvetica", size: 18.0)
    }
    

    【讨论】:

    • Ok 如何确定单元格“var couCell = colorTableView.cellForRowAtIndexPath()”'。我也需要找到第二个单元格
    • 但是当我滚动tableview时它适用于其他单元格。我尝试了很多方式。
    • 谢谢。但是有同样的问题
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-22
    • 1970-01-01
    • 2017-03-02
    • 1970-01-01
    相关资源
    最近更新 更多