我有一个feedVC,它显示来自所有用户的更新。我正在从防火墙中检索数据,并希望根据post长度自动设置高度。问题是,即使在从UITableViewAutomaticDimension返回heightForRowAt之后,当VC第一次启动时,单元也不会自动调整大小。如果我导航到其他vc并返回到feedVc,它就正常工作了。附像
首次启动VC时的()

从选项卡切换到另一个VC,然后再次点击提要,它将给出所需的结果.

class FeedViewController: UIViewController, UITableViewDelegate, UITableViewDataSource{
@IBOutlet weak var tableviewFeed: UITableView!
var listPost = [Posts]()
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return listPost.count
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableviewFeed.dequeueReusableCell(withIdentifier: "feedCell") as! FeedCell
cell.lblUser.sizeToFit()
cell.lblPost.sizeToFit()
cell.lblUser.text = listPost[indexPath.row].user
cell.lblPost.text = listPost[indexPath.row].post
return cell
}
override func viewDidLoad() {
super.viewDidLoad()
tableviewFeed.estimatedRowHeight = 100
}
override func viewWillAppear(_ animated: Bool) {
FirebaseService.instance.retrievePost { (post) in
self.listPost = post.reversed()
self.tableviewFeed.reloadData()
}
}
}发布于 2018-04-19 05:54:22
你需要打电话
cell.lblUser.layoutIfNeeded()之后
cell.lblUser.sizeToFit()https://stackoverflow.com/questions/49913523
复制相似问题