OGeek|极客世界-中国程序员成长平台

标题: ios - 嵌套表和 Collection View 的自定尺寸单元格 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 20:35
标题: ios - 嵌套表和 Collection View 的自定尺寸单元格

我正在尝试构建一个表格 View ,此 TableView 中的一个单元格是一个选项卡小部件。 所以我将这个选项卡小部件创建为 CollectionView。 collectionView 的单元格有不同的 subview ,每个单元格都有自己的大小。

我想要实现的是在 collectionView 中调整单元格大小,但它不起作用。

我会为你提供我使用的方式,以便我可以得到你的帮助。

//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
    }

注意:collectionView 单元格的内容可能是 webView 或 tableview。



Best Answer-推荐答案


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

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

你应该在estimatedHeightForRowAtIndexPath中使用UITableViewAutomaticDimension

希望这对你来说很好......!

关于ios - 嵌套表和 Collection View 的自定尺寸单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52329027/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4