Ios 如何根据节中NumberOfItems的数量设置行的高度?

Ios 如何根据节中NumberOfItems的数量设置行的高度?,ios,swift,uitableview,uicollectionview,Ios,Swift,Uitableview,Uicollectionview,我正在使用UITableViewController并将UICollectionView放在表视图单元格的第二行。问题是我怎样才能动态地获得划船高度 我已尝试使用返回UITableViewAutomaticDimension。但不起作用 非常感谢您的帮助 viewDidLoad override func viewDidLoad() { super.viewDidLoad() self.tableView.estimatedRowHeight = self.ta

我正在使用
UITableViewController
并将
UICollectionView
放在表视图单元格的第二行。问题是我怎样才能动态地获得划船高度

我已尝试使用
返回UITableViewAutomaticDimension
。但不起作用

非常感谢您的帮助

viewDidLoad

override func viewDidLoad() {
        super.viewDidLoad()

        self.tableView.estimatedRowHeight = self.tableView.rowHeight
        self.tableView.rowHeight = UITableViewAutomaticDimension
    }
UITableViewController

选择1

选择2

目前,我硬编码返回的数量

UICollectionViewDataSource

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 15
    }
UICollectionViewDelegate,UICollectionViewDelegateFlowLayout

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {

        let layout = collectionViewLayout as! UICollectionViewFlowLayout
        layout.minimumLineSpacing = 2.5
        layout.minimumInteritemSpacing = 2.5

        itemWidth = (collectionView.bounds.width - 5.0) / 3.0
        return CGSize(width: itemWidth, height: itemWidth)
    }
下面是使用
返回self.tableView.bounds.width+15的UI图像:

以下是使用返回UITableViewAutomaticDimension的UI的图像:

以下是约束的图像:

在您的viewDidLoad()中尝试添加此-:

 override func viewDidLoad() {
        super.viewDidLoad()

       yourTableView.estimatedRowHeight = 70 // this is your storyboard default cell height 
       yourtableView.rowHeight = UITableViewAutomaticDimension //Automatic dimensions

    }
那么试试这个-:

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

在您的情况下,您必须根据集合视图的内容高度为具有集合视图的单元格指定动态高度,但在加载表视图时,可能会在某个时间加载集合视图,因此此时集合视图的内容高度可能不正确

为了获得正确的内容高度,您必须计算集合视图的内容高度,如下所示:

    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {

//Your cell which has a collectionview
    if indexPath.row == 1{ 
    let contentHeight = (numberOfCollectionViewcell*itemHeight)+collectionViewContentOffset
    return contentHeight
    }
            return self.tableView.bounds.width + 15
        }
  • 从CollectionViewDelegate,您可以确定collectionview何时已完全加载

  • 获取
    CollectionView
    内容高度

  • 通过创建委托,将此内容高度发送到
    tableView
    VC

  • 在委托方法
    中重新加载tableView

  • tableViewCell
    CollectionView
    中,已在重新加载,因此每次执行上述操作时,编译器都会无限大地重新加载tableView和CollectionView。为此,您需要添加一个标志(
    isforcelyreloadcollectionview
    ),当tableview为tableview时,该标志为true,否则需要重新加载tableview为false。您需要在强制重新加载tableView的位置添加此标志


  • 如果你需要进一步的帮助,你可以问。如果我有任何其他解决方案,我会更新您。

    您对UICollectionView的限制是什么?@TusharSharma请参阅更新的问题。全部设置为0。您可以演示如何使用自动尺寸标注吗?我的意思是你执行了哪些步骤?我已经更新了问题。目前我正在做的是设置
    返回self.tableView.bounds.width+15
    。长高。如果我使用
    UITableViewAutomaticDimension
    它将是正常的表。@TusharSharma,我已经更新了问题。
    self.tableView.estimatedRowHeight=self.tableView.rowHeight
    self.tableView.rowHeight=UITableViewAutomaticDimension
    我已经实现了这个功能。很抱歉忘记通知您结果。它与图像中返回UITableViewAutomaticDimension的情况相同。您的建议是什么?我认为您无法在UITableView协议中实现此
    collectionViewContentOffset
    。collectionViewContentOffset是一个固定值,由情节提要或代码设置,因此我们可以通过引用collection view获得它在课堂上的任何地方。我应该调用什么来重新加载tableview
    self.tableView.reloadData
    ?@Nizzam是的,但在您使用tableView出口的类中。在
    UICollectionViewDelegate的协议下。在
    func….sizeForItemAt….
    中,我尝试
    打印(collectionView.contentSize.height)
    。它返回0。@Nizzam(uicollectionview已完全加载)[首先让collectionview完全加载,然后检查contentview。
    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
            return UITableViewAutomaticDimension
        }
    
        override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    
    //Your cell which has a collectionview
        if indexPath.row == 1{ 
        let contentHeight = (numberOfCollectionViewcell*itemHeight)+collectionViewContentOffset
        return contentHeight
        }
                return self.tableView.bounds.width + 15
            }