【问题标题】:How to change the size of the label automatically in table cells?如何在表格单元格中自动更改标签的大小?
【发布时间】:2017-07-24 11:16:17
【问题描述】:

如何在表格单元格中自动更改标签的大小?

我已经写好了

    tableView.rowHeight = UITableViewAutomaticDimension

    tableView.estimatedRowHeight = 240

viewDidLoad()

override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return UITableViewAutomaticDimension
}

并且已经将“Lines”设置为 0 以在多行中显示文本。

所以,如果我之前设置了文本,它会在几行中显示文本并自动更改标签的大小,但是如果我显示输入文本并且标签显示我输入的内容,它会变成一行并且不会改变标签的大小。

我还需要做什么?

谢谢。

【问题讨论】:

  • 您需要重新加载该单元格! , 每次更改发生后,您需要重新加载该单元格以反映 UI,也不要忘记编写 layoutIfNeeded
  • 感谢您的评论。如何重新加载该单元格?
  • 你有当前编辑单元格的 indexpath 然后重新加载 indexPath 然后t.reloadRows(at: [IndexPath], with: UITableViewRowAnimation.init(0))
  • 非常感谢!刚刚重装解决了!

标签: ios uitableview swift3


【解决方案1】:

试试这个来更新一个特定的 UITableView 单元:

func updateCell(path:Int){
    let indexPath = NSIndexPath(forRow: path, inSection: 1)

    tableView.beginUpdates()
    tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic) //try other animations
    tableView.endUpdates()
}

【讨论】:

    【解决方案2】:

    为了realod,我添加了

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        tableView.reloadData()
    }
    

    谢谢!

    【讨论】:

      【解决方案3】:

      希望它可以帮助您在视图中编写这两行代码

      tableView.estimatedRowHeight = 80
      tableView.rowHeight = UITableViewAutomaticDimension
      

      【讨论】:

      • 对不起,我的代码部分很难阅读,但我已经完成了。你还有什么想法?
      猜你喜欢
      • 1970-01-01
      • 2013-02-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多