如何解决嵌入在 UIview 中的 tableViewHeight 不会基于水平集合视图高度增加
我在屏幕底部创建了一个非动态 UIview,因为我创建了另一个固定高度的子视图和带有一些随机高度 (10) 的表格视图,并在视图控制器中为其提供了一个出口。在表视图单元格中,我嵌入了一个水平集合视图,并给出了一些特定的高度 (10),并在表视图单元格中为该集合视图高度约束提供了一个出口。每当表格视图单元格重新加载时,我都会根据集合视图内容增加表格视图高度,但它不起作用。令人惊讶的是,我试图将垂直集合视图嵌入到该表格视图单元格中,但当我将滚动方向更改为垂直时,表格视图高度没有改变。下面是代码。
@IBOutlet weak var tbl_offers: UITableView!
@IBOutlet weak var hei_tbl_offers: NSLayoutConstraint!
//added the below code in view did load
self.tbl_offers.register(UINib(nibName: "OffersTblCell",bundle: nil),forCellReuseIdentifier: "OffersCell")
self.tbl_offers.dataSource = self
self.tbl_offers.delegate = self
self.tbl_offers.layoutIfNeeded()
self.tbl_offers.reloadData()
self.view.layoutIfNeeded()
self.hei_tbl_offers.constant = self.tbl_offers.contentSize.height
self.view.layoutIfNeeded()
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
self.hei_tbl_offers.constant = self.tbl_offers.contentSize.height
}
func tableView(_ tableView: UITableView,numberOfRowsInSection section: Int) -> Int {
return 1
}
func tableView(_ tableView: UITableView,cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "OffersCell") as! OffersTblCell
cell.frame = tableView.bounds
cell.layoutIfNeeded()
cell.col_offer.reloadData()
cell.coll_height.constant = cell.col_offer.collectionViewLayout.collectionViewContentSize.height
hei_tbl_offers.constant = tbl_offers.contentSize.height + cell.col_offer.collectionViewLayout.collectionViewContentSize.height
cell.layoutIfNeeded()
self.view.layoutIfNeeded()
return cell
}
func tableView(_ tableView: UITableView,estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableView.automaticDimension
}
func tableView(_ tableView: UITableView,heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableView.automaticDimension
}
//here is the tableViewCell code
class OffersTblCell: UITableViewCell {
@IBOutlet weak var col_offer: UICollectionView!
@IBOutlet weak var coll_height:NSLayoutConstraint!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
//self.coll_height.constant = 190
self.col_offer.register(UINib.init(nibName: "OffersCell",forCellWithReuseIdentifier: "OffersCell")
//self.col_offer.register(UINib.init(nibName: "PopularDestinationCell",forCellWithReuseIdentifier: "PopularDestinationCell")
self.col_offer.delegate = self
self.col_offer.dataSource = self
coll_height.constant = col_offer.collectionViewLayout.collectionViewContentSize.height
}
}
extension OffersTblCell: UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout{
func collectionView(_ collectionView: UICollectionView,numberOfItemsInSection section: Int) -> Int {
return 3
}
func collectionView(_ collectionView: UICollectionView,cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "OffersCell",for: indexPath) as! OffersCell
return cell
}
func collectionView(_ collectionView: UICollectionView,layout collectionViewLayout: UICollectionViewLayout,sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: collectionView.frame.size.width/2 - 16,height: collectionView.frame.size.width/2)
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。