2017-07-24 104 views
1

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

我已经写

tableView.rowHeight = UITableViewAutomaticDimension 

    tableView.estimatedRowHeight = 240 
viewDidLoad()

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

,并已经设置 “线条” 0显示多行文本。

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

我还需要做什么?

谢谢。

+0

您需要重新加载该单元格! ,在每次更改发生之后,您需要重新加载该单元以反映UI,也不要忘记编写layoutIfNeeded –

+0

感谢您的评论。我如何重新加载该单元格? – Yutaro

+0

你有当前编辑单元格的indexpath,然后重新加载该indexPath,然后't.reloadRows(at:[IndexPath],with:UITableViewRowAnimation.init(0))' –

回答

-1

希望它可以帮助你写鉴于这两条线没有负载

tableView.estimatedRowHeight = 80 
tableView.rowHeight = UITableViewAutomaticDimension 
+0

对不起,我的代码部分很难阅读,但我已经完成了。你还有什么想法? – Yutaro

0

试试这个更新特定的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() 
} 
0

要realod,我加

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

谢谢!

相关问题