【问题标题】:How to set UITableViewCell Height according to UICollectionView Content size?如何根据 UICollectionView 内容大小设置 UITableViewCell 高度?
【发布时间】:2017-01-03 14:30:30
【问题描述】:

我有一个 UITableView,里面有两个UICollectionViews

我的要求是我必须先水平滚动UICollectionView。 我必须垂直滚动UITableView,因为我不想滚动第二个UICollectionView

在图像中你可以看到第一个(标题'文件夹')UICollectionView我必须水平滚动,第二个(标题'产品')UICollectionView我不想垂直滚动,因为我必须滚动完成@ 987654329@.

collectionView 都在 TableViewCell 中,我已禁用第二个 UICollectionView 的滚动。

我必须添加和删除一个产品,然后我该如何设置内容应该出现的TableViewSecond 单元格高度。

这是我的代码:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    if (0 == indexPath.section) {
        var addFolderCell = tableView.dequeueReusableCell(withIdentifier: "AddFolderCell") as? AddFolderCell

        if(nil == addFolderCell) {
            let nib:Array = Bundle.main.loadNibNamed("AddFolderCell", owner: self, options: nil)!
            addFolderCell = nib[0] as? AddFolderCell
        }

        addFolderCell?.collectionView.delegate = self
        addFolderCell?.collectionView.dataSource = self

        return addFolderCell!
    } else {
        let identifier = "CreateFolderCell"
        var createFolderCell = tableView.dequeueReusableCell(withIdentifier: identifier) as? CreateFolderCell

        if(createFolderCell == nil) {
            let nib:Array = Bundle.main.loadNibNamed("CreateFolderCell", owner: self, options: nil)!
            createFolderCell = nib[0] as? CreateFolderCell
        }

        return createFolderCell!
    }
}

//Height for row.
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    //let index : NSIndexPath = NSIndexPath(row: 0, section: 1)
    if 0 == indexPath.section {
        return 135
    }
    return 300
}  

//页眉高度。

func tableView(_ tableView: UITableView, estimatedHeightForHeaderInSection section: Int) -> CGFloat {
    return 20
}

func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    return sectionsArray[section] as? String
 }
}

extension GroupDataView : UICollectionViewDelegate,UICollectionViewDataSource {
  func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
      return 4
  }

 func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "AddFolderCollectionCell", for: indexPath) as! AddFolderCollectionCell

    //cell.backgroundColor = UIColor.white
    cell.layer.borderWidth = 0.4
    cell.layer.borderColor = UIColor.lightGray.cgColor
    cell.layer.cornerRadius = 2
    //CreateCardView(viewCorner: cell.cardView) //cell.cardView
    return  cell
  }
}  

//MARK:集合视图布局

extension GroupDataView : UICollectionViewDelegateFlowLayout {

 func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
     return CGSize(width: 90, height: 130)
  }
}

我能做什么?如何设置 tableView 的高度?

【问题讨论】:

  • tableView.rowHeight ?如故事板中定义的那样?
  • 是的,但是我该如何设置,因为第二个单元格高度根据第二个 collectionView 内容大小。谢谢
  • 看起来你的方向是正确的,因为重写 ViewLayout 方法应该可以完成工作。问题是将集合视图单元格的高度传递给表格视图单元格高度?
  • 是的。我能怎么做?谢谢
  • 一个 UITableViewCell 持有一个 UICollectionView,现在你的用例是当你删除一个 UITableViewCell(持有 UICollectionView)你不知道 UITableViewCell 的高度取决于 UICollectionViewCell 高度?

标签: ios swift uitableview uicollectionview contentsize


【解决方案1】:

我认为您可以通过几个步骤完成此操作:

  1. 子类化你的UITableViewCell,我们将其命名为CollectionCell,并将其在IB接口中的类更改为新的子类名称。

  2. 创建从UICollectionViewCollectionCell 子类的出口,其中放置UICollectionView。我们把它命名为collectionView

  3. collectionView 的所有边的约束添加到单元格边界。 (最佳, 右、下、左)。

  4. 在 IB 中添加 collectionView 高度约束并创建到 CollectionCell 的出口。我们将其命名为collectionViewHeight

  5. tableView:cellForRowAtIndexPath:添加代码:

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        // ...
        // normal cell declarations you wish
        // ...
    
        (cell as! CollectionCell).collectionView.reloadData()
        let height = cell.collectionView.collectionViewLayout.collectionViewContentSize.height
        (cell as! CollectionCell).collectionViewHeight.constant = height
        return cell
    }
    
    func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
        return 100 // <- Your Desired Height
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return UITableViewAutomaticDimension
    }
    

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-02-22
    • 2016-06-03
    • 2020-04-02
    • 1970-01-01
    • 1970-01-01
    • 2016-03-13
    • 2022-09-27
    • 2016-11-24
    相关资源
    最近更新 更多