Я не понимаю, почему cellForRowAt indexPath
не позвонил. Я проверил delegate, datasource
, что это правильно. И я также проверяю статическую высоту, тогда она будет работать, но когда я дал tableView height constant
из размера содержимого 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 ответ
Попробуй это
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. Дважды проверьте делегат источника данных.
Похожие вопросы
Новые вопросы
ios
iOS - мобильная операционная система, работающая на Apple iPhone, iPod touch и iPad. Используйте этот тег [ios] для вопросов, связанных с программированием на платформе iOS. Используйте связанные теги [target-c] и [swift] для проблем, характерных для этих языков программирования.