【问题标题】:Self sizing cells for nested table and collection Views嵌套表和集合视图的自定尺寸单元格
【发布时间】:2018-09-14 17:45:19
【问题描述】:

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

我想要实现的是在 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。

【问题讨论】:

    标签: ios swift uitableview dynamic uicollectionviewcell


    【解决方案1】:
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
            return 50
        }
    
    func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
            return UITableViewAutomaticDimension
        }
    

    你应该在estimatedHeightForRowAtIndexPath中使用UITableViewAutomaticDimension

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

    【讨论】:

    • 没有用。但我已经在viewDidLoad 中添加了这两行。 tableView.rowHeight = UITableViewAutomaticDimension tableView.estimatedRowHeight = 300
    • 你能编辑你的答案并分享你的屏幕截图你的设计是什么样的
    猜你喜欢
    • 2019-12-04
    • 1970-01-01
    • 2018-03-24
    • 2020-10-04
    • 1970-01-01
    • 1970-01-01
    • 2017-03-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多