将标签放置在collectionview内,并将进一步的collectionview放置在具有动态高度的tableview单元格内

问题描述

tableview单元格内的collectionview不能适应动态高度,就像我希望tableview单元格应采用collectionview单元格内标签的大小一样(标签的约束是前导,试用,顶部,底部是5倍于collectionview单元格的contentView)

    class myRewardsViewController: UIViewController,UITableViewDataSource,UITableViewDelegate {




@IBOutlet weak var rewardsTableView: UITableView!
func tableView(_ tableView: UITableView,numberOfRowsInSection section: Int) -> Int {
    return 1
}

func tableView(_ tableView: UITableView,cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    
        let cell = tableView.dequeueReusableCell(withIdentifier: "rewardsTV_cell",for: indexPath) as! rewardsCarouselTableViewCell
       
        cell.layoutIfNeeded()
        cell.carouselRewardsCV.reloadData()
        cell.rewardCarouselHeight.constant = cell.carouselRewardsCV.collectionViewLayout.collectionViewContentSize.height
        return cell
    
   
}

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()
    rewardsTableView.reloadData()
}
override func viewDidLoad() {
    super.viewDidLoad()
    rewardsTableView.estimatedRowHeight = 300
    rewardsTableView.rowHeight = UITableView.automaticDimension
    // Do any additional setup after loading the view.
}

func tableView(_ tableView: UITableView,heightForRowAt indexPath: IndexPath) -> CGFloat {
    return UITableView.automaticDimension
}
/*
// MARK: - Navigation

// In a storyboard-based application,you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue,sender: Any?) {
    // Get the new view controller using segue.destination.
    // Pass the selected object to the new view controller.
}
*/

}

class rewardsCarouselTableViewCell: UITableViewCell,UICollectionViewDelegate,UICollectionViewDataSource{

@IBOutlet weak var carouselRewardsCV: UICollectionView!
@IBOutlet weak var rewardCarouselHeight: NSLayoutConstraint!

func collectionView(_ collectionView: UICollectionView,numberOfItemsInSection section: Int) -> Int {
    return 5
    
}

func collectionView(_ collectionView: UICollectionView,cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "rewardsCV_cell",for: indexPath) as! rewardscarouselCollectionViewCell
    return cell
}


override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code
    
    carouselRewardsCV.delegate = self
    carouselRewardsCV.dataSource = self
    
    //carouselRewardsCV.selfSi
}
override func layoutSubviews() {
    super.layoutSubviews()
    layoutIfNeeded()
}

override func setSelected(_ selected: Bool,animated: Bool) {
    super.setSelected(selected,animated: animated)

    // Configure the view for the selected state
}

}

 extension rewardsCarouselTableViewCell: 
 UICollectionViewDelegateFlowLayout{
func collectionView(_ collectionView: UICollectionView,layout collectionViewLayout: UICollectionViewLayout,sizeforItemAt indexPath: IndexPath) -> CGSize {
    return CGSize(width: collectionView.frame.width,height: collectionView.frame.height)
}

}

DynamicHeightRewardsCollectionView类:UICollectionView {

override func layoutSubviews() {
    super.layoutSubviews()
    if bounds.size != intrinsicContentSize {
        self.invalidateIntrinsicContentSize()
    }
}
override var intrinsicContentSize: CGSize {
    return collectionViewLayout.collectionViewContentSize
}

}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)