如何根据UICollectionView内容大小设置UITableViewCell高度?

时间:2017-01-03 14:30:31

标签: ios swift uitableview uicollectionview contentsize

我有一个 UITableView 和Inside,我有两个UICollectionViews

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

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

两个collectionView都位于TableViewCell,我已禁用第二个UICollectionView的滚动。

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

enter image description here

enter image description here

这是我的代码:

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的高度?

1 个答案:

答案 0 :(得分: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
    }
    
相关问题