简体   繁体   English

UITableViewCell内的UICollectionView如何根据屏幕大小调整tableview单元格高度

[英]UICollectionView inside UITableViewCell how to adjust tableview cell height based on screen size

I have the following layout我有以下布局

  • UITableView用户界面
    • UITableViewCell (class: CategoryCell) UITableViewCell(类:CategoryCell)
      • Label标签
      • Button按钮
      • UICollectionView用户界面集合视图
        • UICollectionViewCell (class: ItemCell) UICollectionViewCell(类:ItemCell)
          • UIImageView用户界面图像视图
          • UILabel用户界面标签

I am trying to make UICollectionView to always show 3 items no matter the screen size.我试图让 UICollectionView 始终显示 3 个项目,无论屏幕大小如何。 I was able to get items to be shown based on screen width and was able to set the height.我能够根据屏幕宽度显示项目并能够设置高度。 However, the height of table view does not change from inside CategoryCell.但是,表格视图的高度不会从 CategoryCell 内部改变。 Here is the code:这是代码:

// CategoryCell.swift

func awakeFromNib() {
    super.awakeFromNib()

    // Calculate width and height based on screen width
    let screenWidth = UIScreen.main.bounds.width
    let itemWidth = screenWidth / 3.0
    let itemHeight = itemWidth / 0.75 // 3:4 aspect ratio

    // Change height of table view cell
    self.bounds.size.height = self.bounds.size.height - collectionView.bounds.size.height + itemHeight
    // This is the line does nothing
    // It should remove collectionView height from tableView (height of collectionView is set through autolayout) then add he new height to it.

    // Set collection view height to equal to item height
    collectionView.bounds.size.height = itemHeight

    // set item height
    let layout = collectionViewProducts.collectionViewLayout as! UICollectionViewFlowLayout
    layout.itemSize = CGSize(width: itemWidth, height: itemHeight - 1.0)
    // -1.0 because item/cell height needs to be SMALLER than collection view height
}

How can I change table view cell height from inside the cell class itself?如何从单元格类本身内部更改表格视图单元格高度? My only guess is that I should not be doing these operations inside awakeFromNib but I am not able to find another function to call these from.我唯一的猜测是我不应该在awakeFromNib 中执行这些操作,但是我无法找到另一个函数来调用这些操作。

EDIT: I am using RxSwift, so my data source code is the following:编辑:我使用的是 RxSwift,所以我的数据源代码如下:

let observable = Observable.just(data)
observable.bindTo(tableView.rx.items(cellIdentifier: "CategoryCell", cellType: CategoryCell.self)) { (row, element, cell) in
    cell.setCategory(category: element)
}.disposed(by: disposeBag)

tableView.rx.setDelegate(self).disposed(by: disposeBag)

You could implement UITableViewDelegate 's heightForRowAt and return a value based on a variable.您可以实现UITableViewDelegateheightForRowAt并根据变量返回一个值。 And you can set the variable wherever you do your UICollectionView itemHeight calculation.并且您可以在任何进行UICollectionView itemHeight计算的地方设置变量。 So, when you are done with the calculation, you should be able to do a table view data reload and the layout should update using the new itemHeight value.因此,当您完成计算后,您应该能够重新加载表格视图数据,并且布局应该使用新的itemHeight值进行更新。

I have not tested the code but the above should work.我还没有测试过代码,但上面的应该可以工作。 If you run into any issues, or I've misunderstood your requirements somehow, do let me know.如果您遇到任何问题,或者我以某种方式误解了您的要求,请告诉我。

Provide the constraints to the collection view and make collectionviewcell outlet in tableviewcell page as well as collectionviewheight constraint outlet in the same tableviewcell page.为集合视图提供约束,并在 tableviewcell 页面中创建 collectionviewcell 出口,并在同一 tableviewcell 页面中创建 collectionviewheight 约束出口。 Like this in tableviewcell page:像这样在 tableviewcell 页面中:

class HomeServiceTableCell: UITableViewCell {

  @IBOutlet weak var dealsCollectionView: UICollectionView!
  @IBOutlet weak var dealsCollectionHeight: NSLayoutConstraint!
  @IBOutlet weak var dealsImage: UIImageView!

  // like this in the main page cellforRowAt indexPath function

  func tableView (_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = homeServiceTable.dequeueReusableCell ( withIdentifier:  "homeServiceTableCell6" ) as! homeServiceTableCell6
    cell.dealsImage.image = UIImage(named: "deals.png")
    cell.dealsCollectionView.tag = indexPath.row
    cell.dealsCollectionView.delegate = self
    cell.dealsCollectionView.dataSource = self
    cell.dealsCollectionView.reloadData()
    cell.dealsCollectionHeight.constant = cell.dealsCollectionView.collectionViewLayout.collectionViewContentSize.height

    return cell
  }

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

  // lastly add this line in viewWillAppear function

  override func viewWillAppear(_ animated: Bool) {
    self.homeServiceTable.layoutSubviews()
  }
}

You need to provide tableview's cell height for every cell and based on that you can calculate your collectionview cell's height.您需要为每个单元格提供 tableview 的单元格高度,并基于此计算您的 collectionview 单元格的高度。 for dynamic height of tableview's cell you need to implement these method.对于 tableview 单元格的动态高度,您需要实现这些方法。

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

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

or if you need fixed height then simply use these method and provide your cell height based on your calculation.或者如果您需要固定高度,那么只需使用这些方法并根据您的计算提供您的单元格高度。

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return fixedSize //e.g 100 
}

try this hope it will help you.试试这个希望它会帮助你。

Thank you @Prince and @Fahim.谢谢@Prince 和@Fahim。 Your answers gave me an idea to move my row height logic to tableView delegate.您的回答让我有了将行高逻辑移动到 tableView 委托的想法。 Since, I am calculating the height based on screen width and aspect ratio.因为,我是根据屏幕宽度和纵横比计算高度。 I just moved the same logic to tableView height:我只是将相同的逻辑移动到 tableView 高度:

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    let screenWidth = UIScreen.main.bounds.size.width

    // 3 items + 10px gap between cells
    let itemWidth = (screenWidth / 3.0) - 20

    // 3:4 aspect ratio + other elements
    let itemHeight = (itemWidth) / 0.75 + 110

    return itemHeight
}

For now this solution works normally for me, I would like to change it so it takes into account the cell itself.现在这个解决方案对我来说正常工作,我想改变它,以便它考虑到单元格本身。

I know it is weird to ask a question in the answer but it is related to this thread, so I think it is best to ask it here.我知道在答案中提出问题很奇怪,但它与这个线程有关,所以我认为最好在这里提问。 How can I get the cell object from indexPath?如何从 indexPath 获取单元格对象? Using tableView.cellForRow(at: indexPath) gives me bad access.使用tableView.cellForRow(at: indexPath)让我无法访问。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何基于其中的Web视图的html内容调整tableView单元格的高度? - How to adjust the height of a tableView Cell based on html content of the web-view inside it? 表格视图单元格内的表格视图自动调整单元格高度 - tableview inside tableview cell adjust cell height automatically TableView单元格内的TableView,自动调整单元格高度 - TableView inside TableView cell, adjust cell height automatically 如何将 UICollectionView 的高度调整为 UICollectionView 的内容大小的高度? - How to adjust height of UICollectionView to be the height of the content size of the UICollectionView? 更改基于 UICollectionView 内容大小高度的 UITableViewCell 高度 - Change UITableViewCell Height based UICollectionView content size height 如何在uitableviewcell内设置uicollectionview的高度? - How to set the height of a uicollectionview inside an uitableviewcell? 快速扩展Tableview单元格如何调整高度 - Swift expanding Tableview cell how to adjust height UICollectionView Cell Spacing 基于设备屏幕尺寸 - UICollectionView Cell Spacing based on device screen size Swift:根据其中的UICollectionView的大小扩展UITableViewCell高度 - Swift: Expand UITableViewCell height depending on the size of the UICollectionView inside it UITableViewCell 内的 UICollectionView — 动态高度? - UICollectionView inside a UITableViewCell — dynamic height?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM