我正在实现内部包含 UICollectionView
的 UITableView
。当然,我的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/