微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决将标签放置在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 举报,一经查实,本站将立刻删除。