ios - 如何快速显示每个单元格附近的Collectionview单元格
问题描述
我需要一个相邻的单元格,如下所示
但我是这样的:在短电话中,它们彼此相距甚远
对于collectionview,我在故事板中给出了这样的约束:
height = 80, leading = trailing = 0, top = 20
在 viewdidload 中:
override func viewDidLoad() {
super.viewDidLoad()
//services collectionview cell
let serviceLayout = UICollectionViewFlowLayout()
serviceLayout.minimumInteritemSpacing = 0
serviceLayout.minimumLineSpacing = 0
serviceLayout.estimatedItemSize = UICollectionViewFlowLayout.automaticSize
self.servicesCollectionView.collectionViewLayout = serviceLayout
servicesCollectionView.contentInsetAdjustmentBehavior = .always
servicesCollectionView.addObserver(self, forKeyPath: "contentSize", options: .new, context: nil)
}
除了这个,我还没有给出更多的东西..请帮忙..让collectionview单元格靠近每个单元格
编辑:
因为servicesCollectionView
我已经评论了 didload 中的所有内容并添加了这样的内容.. 然后它的工作
servicesCollectionView.addObserver(self, forKeyPath: "contentSize", options: .new, context: nil)
对于这个单元格,我需要彼此靠近,如何做到这一点..请建议
galleryCollectionView.collectionViewLayout = LeftAlignCellCollectionFlowLayout()
let galleryLayout = UICollectionViewFlowLayout()
galleryLayout.scrollDirection = .horizontal
self.galleryCollectionView.collectionViewLayout = galleryLayout
self.galleryCollectionView!.contentInset = UIEdgeInsets(top: 0, left: 0, bottom:0, right: 0)
let heightGallery = 120
let widthGallery = view.frame.size.width
if let galleryLayout = self.galleryCollectionView.collectionViewLayout as? UICollectionViewFlowLayout {
galleryLayout.minimumInteritemSpacing = 0
galleryLayout.minimumLineSpacing = 0
galleryLayout.itemSize = CGSize(width: widthGallery * 0.7, height: CGFloat(heightGallery))
}
解决方案
您需要子类UICollectionViewFlowLayout
化才能获得此行为。
import UIKit
class LeftAlignCellCollectionFlowLayout: UICollectionViewFlowLayout {
private(set) var cellHeight: CGFloat = 36
init(cellHeight: CGFloat) {
super.init()
self.cellHeight = cellHeight
}
required init?(coder: NSCoder) {
super.init(coder: coder)
}
override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
guard let attributes = super.layoutAttributesForElements(in: rect) else { return nil }
guard let collectionView = self.collectionView else { return nil }
self.estimatedItemSize = UICollectionViewFlowLayout.automaticSize
self.minimumLineSpacing = 0
self.minimumInteritemSpacing = 2
var newAttributes = attributes
var leftMargin = self.sectionInset.left
var maxY: CGFloat = -1.0
let availableWidth: CGFloat = collectionView.frame.width
let layout = collectionView.collectionViewLayout
for attribute in attributes {
if let cellAttribute = layout.layoutAttributesForItem(at: attribute.indexPath) {
if cellAttribute.frame.width > availableWidth {
cellAttribute.frame.origin.x = 0
cellAttribute.frame.size = CGSize(width: availableWidth, height: cellHeight)
}
else {
if cellAttribute.frame.origin.y >= maxY {
leftMargin = self.sectionInset.left
}
var frame = cellAttribute.frame
frame.origin.x = leftMargin
frame.size.height = cellHeight
cellAttribute.frame = frame
leftMargin += cellAttribute.frame.width + self.minimumInteritemSpacing
maxY = max(cellAttribute.frame.maxY , maxY)
}
newAttributes.append(cellAttribute)
}
}
return newAttributes
}
}
这是你如何使用它。
collectionView.collectionViewLayout = LeftAlignCellCollectionFlowLayout(cellHeight: 40)
推荐阅读
- javascript - Asp.net 控制器收到空值
- angular - 浏览器挂起问题 - 在处理具有 7000 个节点的大型 Angular 材质树时
- php - 如何在 PHP 中计算/求和相同单词的值?
- java - 会话未在 websphere 内存中复制到内存复制
- websphere-liberty - 重新部署 JavaEE 8 EAR 文件后,Websphere Liberty 配置文件上的 LD_LIBRARY_PATH 不再已知
- javascript - HTML 文件中 SVG 的超链接
- mysql - Mysql问题有1100万行数据
- javascript - 如何在 javscript 中将浮点数 0.0521000 更改为 5.21
- jquery - 如何从循环中获取 td 中的值
- c++ - 一次包含已在 main.obj 中定义的 .h 函数