ios - 嵌套表格和 Collection View 的 self 调整单元格

标签 ios swift uitableview dynamic uicollectionviewcell

我正在尝试构建一个 TableView ,此 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。

最佳答案

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 的 self 调整单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52329027/

相关文章:

ios - 如何使状态栏与导航栏颜色相同?

ios - 在 Swift 中处理未捕获的异常 valueForUndefinedKey

iphone - 通过使用低分辨率图像然后交换高分辨率图像来加速 UIImagePickerController

iphone - 重复的 UItableViewCell,UITableView 的行

ios - 如何在 swift 中正确缓存 NSURLRequest?

swift - 使用空指针列解析保存对象

ios - 使用 PresentViewController 而不是 Storyboard 不会显示单元格数据?

iOS UItableviewCell 清除旧的单元格值

ios - 删除 TableView 中的最后一行并在新单元格中显示 "no results"标签

ios - 想要使用 ios sdk 中的 AirPlay 应用程序在 Apple TV 上流式传输我的应用程序 View ?