【问题标题】:UITableViewCell not able to adjust height based on the dynamic CollectionView it containsUITableViewCell 无法根据它包含的动态 CollectionView 调整高度
【发布时间】:2018-02-26 10:23:25
【问题描述】:

让我从解释视图层次结构开始。 我使用的是普通的 UITableView,在 UITableViewCell 内有一个高度动态的 collectionView。

我如何在 UITableView 的 willDisplay 单元格中为 UICollectionView 设置数据源和委托:

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {

    if indexPath.section == 0 {

        guard let collectionCell = cell as? movieListingTableViewCell else { return }

        collectionCell.setCollectionViewDataSourceDelegate(self, forRow: indexPath.row)
        collectionCell.selectionStyle = .none
    }
}

collectionView 具有顶部、底部、前导、尾随和高度约束。

然后我在 UITableView 单元子类中使用高度约束并将其设置为 collectionView 高度,

override func layoutSubviews() {

    timingCollectionHeight.constant = timingCollection.collectionViewLayout.collectionViewContentSize.height
}

发生的情况是我的单元格没有正确显示,因为有空格。例如,如果有一个 UITableView 单元格的 collectionView 高度很大,那么其他 UITableView 单元格也会以某种方式使用相同的高度。滚动表格后,单元格显示正确。

【问题讨论】:

    标签: ios swift uitableview uicollectionview


    【解决方案1】:

    Swift 示例

    首先,在你的 tableViewCell 类中获取你的 collectionViewHeightConstraint 的出口,如下所示:-

    @IBOutlet weak var collectionViewHeight: NSLayoutConstraint!
    

    然后在 ViewController 类的 viewDidLoad() 方法中添加以下代码以获取 tableViewCell 的动态高度:-

    myTableView.estimatedRowHeight = 100;  // Default tableViewCell height
    myTableView.rowHeight = UITableViewAutomaticDimension;
    

    然后在您的 tableView cellForRowAtindexPath 委托中添加以下代码以根据 CollectionView 高度更新 tableView Cell 的高度:-

    cell.frame = tableView.bounds;  // cell of myTableView
    cell.layoutIfNeeded()
    cell.myCollectionView.reloadData()
    cell.collectionViewHeight.constant = cell.myCollectionView.collectionViewLayout.collectionViewContentSize.height;
    

    希望这会有所帮助。 快乐编码:)

    【讨论】:

      【解决方案2】:

      另一种方法: 将 UITableView 单元格高度设置为动态后。

           tableView.estimatedRowHeight = 100
           tableView.rowHeight = UITableViewAutomaticDimension
      
      1. 为collectionView创建一个子类并覆盖intrinsicContentSize。

        class DynamicCollectionView: UICollectionView {
            override func layoutSubviews() {
                 super.layoutSubviews()
                if !__CGSizeEqualToSize(bounds.size, self.intrinsicContentSize) {
                    self.invalidateIntrinsicContentSize()
                }
            }
        
            override var intrinsicContentSize: CGSize {
                return collectionViewLayout.collectionViewContentSize
            }
        }
        
      2. 在 Interface builder 中,将 collectionView 的类更改为 DynamicCollectionView(子类 UICollectionView)。

      3. 设置 UICollectionViewFlowLayout 的估计单元格大小。

            flowLayout.estimatedItemSize = CGSize(width: 1,height: 1)
        

      【讨论】:

        猜你喜欢
        • 2015-03-25
        • 2016-12-06
        • 1970-01-01
        • 2011-03-05
        • 2010-11-03
        • 1970-01-01
        • 2016-05-12
        • 1970-01-01
        • 2016-09-03
        相关资源
        最近更新 更多