我正在尝试创建一个具有标题单元格(动态调整大小)和一些“正常”内容单元格(也动态调整大小)的集合视图。因此,collection view初始化如下:
private lazy var timelineCollectionView: UICollectionView = {
let flowLayout = UICollectionViewFlowLayout()
flowLayout.scrollDirection = .vertical
flowLayout.sectionInset = UIEdgeInsets(top: self.coloredTitleBarHeight, left: 0, bottom: 0, right: 0) // assume that coloredTitlebarHeight is a constant of 120
flowLayout.estimatedItemSize = CGSize(width: self.view.frame.width, height: 10)
flowLayout.minimumInteritemSpacing = 0
flowLayout.minimumLineSpacing = 0
let cv = UICollectionView(frame: .zero, collectionViewLayout: flowLayout)
cv.alwaysBounceVertical = true
cv.contentInsetAdjustmentBehavior = .never
cv.backgroundColor = .clear
cv.scrollIndicatorInsets = UIEdgeInsets(top: self.coloredTitleBarHeight - self.getStatusBarHeight(), left: 0, bottom: 0, right: 0)
cv.delegate = self
cv.dataSource = self
cv.register(TLContentCell.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "content-header-cell")
cv.register(TLContentCell.self, forCellWithReuseIdentifier: "comment-cell")
return cv
}()
collectionView是ViewController的一部分,该ViewController 符合以下协议:UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout
然后,我使用以下代码创建标题单元格:
func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
let cell = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "content-header-cell", for: indexPath) as! TLContentCell
cell.timelineContent = tlContentItem
cell.delegate = self
return cell
}
最后但并非最不重要的是,常规单元的出队:
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "comment-cell", for: indexPath)
return cell
}
collectionView显示标题单元格和项目单元格,但是,不应用插入部分。
如果您能给我有关这种奇怪行为的任何建议,我将非常高兴。
部分插图仅适用于UICollectionViewFlowLayout中的内容。
使用部分插图来调整内容的边距
部分插图是一种用于调整可用于布置单元格的空间的方法。您可以使用插图在节的标题视图之后插入空格,然后在页脚视图之前。您还可以使用插图在周围插入空格内容的两面。图3-5展示了插图如何影响垂直滚动流布局中的一些内容。 source