ios - 从 contentSize 给出 tableview 高度时未调用 cellForRowAt indexPath
问题描述
我不知道为什么cellForRowAt indexPath
不打电话。我检查delegate, datasource
了它是正确的。而且我还检查了静态高度,然后它会起作用,但是当我constant
从 tableview 内容大小给出 tableView 高度时,我不会打电话。这是我的代码。
//MARK: View life cycle
override func viewDidLoad() {
super.viewDidLoad()
tblItemList.rowHeight = 76
tblItemList.estimatedRowHeight = UITableView.automaticDimension
}
override func viewWillLayoutSubviews() {
heightConstraintForTblItem.constant = tblItemList.contentSize.height
}
extension CheckoutVC : UITableViewDelegate, UITableViewDataSource
{
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return arrProductList.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCell(withIdentifier: "CheckOutItemCell", for: indexPath) as! CheckOutItemCell
if arrProductList.count > 0 {
let data = arrProductList [indexPath.row]
cell.lblItemName.text = data.product_name ?? ""
cell.lblQuantity.text = "\(data.product_total_quantity ?? 0)"
cell.lblTotalPrice.text = CURRENCY + "\(data.product_total_amount ?? 0.0)"
}
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if tableView == tblItemList {
return UITableView.automaticDimension
}
}
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableView.automaticDimension
}
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
viewWillLayoutSubviews()
}
}
解决方案
尝试这个
1)删除func tableView(_ tableView: UITableView, estimatedHeightForRowAt
方法和func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat
2)删除viewWillLayoutSubviews
方法
3) 在ViewDidAppear
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
self.view.layoutIfNeeded()
self.heightConstraintForTblItem.constant = self. tblItemList.contentSize.height
self.view.layoutIfNeeded()
}
3)在您的 API 调用之后或当您在 tableview 中加载数据时
self.tblItemList.reloadData()
self.view.layoutIfNeeded()
self.heightConstraintForTblItem.constant = self. tblItemList.contentSize.height
self.view.layoutIfNeeded()
确保给定正确的约束,确保正确连接 IBOutlet,仔细检查数据源委托
推荐阅读
- node.js - 无法在 iis 服务器上部署 Adonis.js 5
- python - 垃圾回收完成后的方法调用
- c# - 在 C# 中为另一个进程停用或禁用控制器/游戏手柄功能
- excel - IF AND 和 IF OR 在同一个公式中
- c# - Dapper 异常 - 当 'buffered' 为 'false' 时,“ConnectionString 属性尚未初始化”
- sql - 使用 SQLAlchemy 优化过滤多个 SQL 计数子查询
- math - 分辨率和重要数字处理
- elasticsearch - script_score 查询不支持 [查询]
- java - 两个 java 应用程序之间的通信在 JBoss 7.0 中正常工作,但在 Tomcat 8.5 中不工作
- geocoding - “Buffalo”查询无法从 /discover 端点返回“Buffalo Exchange”