ios - 什么定义了 UITableViewCell 中 CollectionViewCell 的大小?

标签 ios uitableview uicollectionview

我正在实现内部包含 UICollectionViewUITableView 。当然,我的UITableViewCell有一个UICollectionView,但问题是我不知道如何配置UICollectionViewCell的高度。我尝试设置高度约束以在 UICollectionViewCell 内查看,但它不会产生影响。调整大小的方法有很多种:

  • UITableViewCell 的句柄大小
  • UITableViewCell 内的 UICollectionView 设置高度约束
  • UICollectionViewDelegateFlowLayout的使用方法
  • 设置高度约束以在 UICollectionViewCell 中查看(我的方法)

最后一种方式最适合我,但是我想知道为什么不行。这是UICollectionView的配置:

lazy var booksCollectionView: UICollectionView = {
    let layout = UICollectionViewFlowLayout()
    layout.scrollDirection = .horizontal
    var booksCollectionView = UICollectionView(frame: contentView.bounds, collectionViewLayout: layout)
    booksCollectionView.backgroundColor = .clear
    booksCollectionView.translatesAutoresizingMaskIntoConstraints = false
    return booksCollectionView
}()

约束:

booksCollectionView.trailingAnchor.constraint(equalTo: contentView.trailingAnchor).isActive = true
    booksCollectionView.leadingAnchor.constraint(equalTo: contentView.leadingAnchor).isActive = true
    booksCollectionView.topAnchor.constraint(equalTo: contentView.topAnchor).isActive = true
    booksCollectionView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor).isActive = true

UICollectionView 中的单元格仅包含 UIView:

bookView.leadingAnchor.constraint(equalTo: contentView.leadingAnchor).isActive = true
    bookView.trailingAnchor.constraint(equalTo: contentView.trailingAnchor).isActive = true
    bookView.topAnchor.constraint(equalTo: contentView.topAnchor).isActive = true
    bookView.heightAnchor.constraint(equalToConstant: 440).isActive = true 

改变 bookView 的高度完全没有影响。为什么会发生这种情况?我应该如何处理 UICollectionViewCell 的高度?

最佳答案

遵循协议(protocol):UICollectionViewDelegateFlowLayout 并实现以下方法: collectionView(_:layout:sizeForItemAt:)

关于ios - 什么定义了 UITableViewCell 中 CollectionViewCell 的大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56406417/

相关文章:

ios - 使用 Swift,加密存储对象的最佳方法是什么?

ios - 如何从子类的 uitableview 推送 uiviewcontroller

ios - UITableViewCell 向右滑动触发 Action 或执行 segue

ios - 自定义 UICollectionViewLayout : layoutAttributesForElementsInRect asks for really little rect

ios - 当用户单击 UITextView 并出现键盘时如何使背景变暗/着色?

iphone - CoreData - 关系的一侧始终为零

ios - 如何在函数的返回语句中插入 UIImage View ?

ios - 寄存器有什么问题(_ :forCellWithReuseIdentifier:) on UICollectionView?

ios - 如何在 UICollectionView 的节标题中动态添加标签和按钮?

ios 使用全局导航 Controller