【发布时间】:2020-02-15 02:04:41
【问题描述】:
我不明白为什么cellForRowAt indexPath 不打电话。我检查了delegate, datasource 是正确的。而且我还检查了静态高度然后它会起作用,但是当我从 tableview 内容大小给 tableView 高度constant 时,我不会打电话。这是我的代码。
//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()
}
}
【问题讨论】:
-
设置出口表视图高度,如@IBOutlet weak var heightConstraintForTblItem: NSLayoutConstraint!
-
如果表格视图高度小于单元格高度,则不会调用。请检查 tableview 高度。
-
我认为您的 tableview 高度可能为零或小于单元格大小
-
@NiravKotecha 不,这是正确的
-
@JaydeepVyas 不,它不是零
标签: ios swift iphone uitableview contentsize