简体   繁体   English

嵌套表和集合视图的自定义单元格

[英]Self sizing cells for nested table and collection Views

I'm trying to build a table view, one of the cells in this TableView is a tabs widget. 我正在尝试建立表格视图,此TableView中的单元格之一是tabs小部件。 so I'm creating this tab widget as a CollectionView. 所以我将这个标签小部件创建为CollectionView。 the cells of the collectionView have different subViews and each cell has it's own size. collectionView的单元格具有不同的子视图,每个单元格都有自己的大小。

what I'm trying to achieve is Self-sizing cells in the collectionView, but it is not working. 我想要实现的是collectionView中的自定义大小的单元格,但是它不起作用。

I'll provide you with the way I used so I can get your help. 我将为您提供以前的使用方式,以便您获得帮助。

//MARK: TableView
     func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
            return UITableViewAutomaticDimension
    }

 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "DetailsTabs", for: indexPath) as! DetailsTabs /*this cell has 2 collectionViews. The first collectionView is the tabs and the second collectionView is the content that should be diplayed. I gave the first collectionView a fixed height, trailing leading and top. the second collectionView has trailing, leading bottom, and top to the first collectionView*/
        cell.postDetail = postDetail
        cell.widgets = self.Template
        cell.parentController = self
        cell.Shownavigationbar = self.Shownavigationbar

        return cell
}

//MARK: DetailsTabs Class

 override func awakeFromNib() {
//code to setUp the collectionViews

//pagesLayout is the UICollectionViewFlowLayout for the second collectionView
       pagesLayout.estimatedItemSize = CGSize(width: UIScreen.main.bounds.width, height: 1)
        }

//MARK: IN collectionView Cells I added this function:
 override func preferredLayoutAttributesFitting(_ layoutAttributes: UICollectionViewLayoutAttributes) -> UICollectionViewLayoutAttributes {
        setNeedsLayout()
        layoutIfNeeded()

        let size = contentView.systemLayoutSizeFitting(layoutAttributes.size)
        var frame = layoutAttributes.frame
        frame.size.height = ceil(size.height)
        layoutAttributes.frame = frame

        return layoutAttributes
    }

note: the content of the collectionView cells might be webView or tableview. 注意:collectionView单元格的内容可能是webView或tableview。

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 50
    }

func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
        return UITableViewAutomaticDimension
    }

you should use UITableViewAutomaticDimension in estimatedHeightForRowAtIndexPath 你应该在estimatedHeightForRowAtIndexPath使用UITableViewAutomaticDimension

hope this is working fine for you...! 希望这对您来说很好...!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM