我试图理解为什么集合视图仅将集合中的最后一个单元格居中对齐。
我创建了一个简单的基于流布局的集合视图。我正在使用自动布局标志 - 我不确定是否会导致此问题。
每当我从“集合”视图中删除一个单元格时,前几个单元格似乎工作正常并滚动到左侧。然而,当我删除倒数第二个时,最后一个单元格突然从左对齐变为中心对齐。
有人有解释吗?看起来很奇怪。
我该如何做才能使所有单元格都向左滚动并保持向左对齐。
编辑:这是视图层次结构调试:https://i.stack.imgur.com/mCGzH.jpg
这是视图行为
我做了一个简单的github来演示它:https://github.com/grantkemp/CollectionViewIssue
这是代码:
var dataforCV = ["short","longer phrase", "Super long Phrase"]
override func viewDidLoad() {
super.viewDidLoad()
demoCollectionView.reloadData()
let layout = UICollectionViewFlowLayout()
// Bug Description - > UICollectionViewFlowLayoutAutomaticSize will center Align the layout if it has only a single cell - but it will left align the content if there is more than one cell.
// I would expect this behaviour to be consistently left aligning the content.
//How to repeat: If you comment out UICollectionViewFlowLayoutAutomaticSize then the collection view will show 3 cells being left aligned and it will continue to left align all the content no matter how many cells you remove.
// But: if you leave turn UICollectionViewFlowLayoutAutomaticSize on - then it will intially show 3 cells being left aligned, and then as you click to remove each cell they will stay left aligned until the last single cell will suddenly center align in the collection view
// see here for the screen recording:https://i.stack.imgur.com/bledY.gif
// see here for the view hierachy debuggins screen: https://i.stack.imgur.com/mCGzH.jpg
layout.estimatedItemSize = UICollectionViewFlowLayoutAutomaticSize
// <-- End Bug Description
demoCollectionView.collectionViewLayout = layout
}
//MARk: CollectionView
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as? collectionCell
cell?.text.text = dataforCV[indexPath.row]
return cell!
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return dataforCV.count
}
//Remove the item from the array if selected
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
dataforCV.remove(at: indexPath.row)
demoCollectionView.deleteItems(at: [indexPath])
}
我设法使用以下两个步骤解决了这个问题:
- 向 Apple 记录了一个关于奇怪行为的错误,我注意到使用
UICollectionViewFlowLayoutAutomaticSize
- 我通过创建一个修改后的解决方法
CustomViewFlowLayout
(找不到原始的 SO/github 问题,我在其中看到了这种方法的使用 - 如果我找到它,我会添加它)然后我添加了UICollectionViewFlowLayoutAutomaticSize
设置 - 而且效果很好。
示例代码:
class MyLeftCustomFlowLayout:UICollectionViewFlowLayout {
override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
let attributes = super.layoutAttributesForElements(in: rect)
var leftMargin = sectionInset.left
var maxY: CGFloat = 2.0
let horizontalSpacing:CGFloat = 5
attributes?.forEach { layoutAttribute in
if layoutAttribute.frame.origin.y >= maxY
|| layoutAttribute.frame.origin.x == sectionInset.left {
leftMargin = sectionInset.left
}
if layoutAttribute.frame.origin.x == sectionInset.left {
leftMargin = sectionInset.left
}
else {
layoutAttribute.frame.origin.x = leftMargin
}
leftMargin += layoutAttribute.frame.width + horizontalSpacing
maxY = max(layoutAttribute.frame.maxY, maxY)
}
return attributes
}
在 ViewController 中 - 您必须将流布局添加到集合视图中才能使其工作:
let layout = MyLeftCustomFlowLayout()
layout.estimatedItemSize = UICollectionViewFlowLayoutAutomaticSize
myCollectionViewReference.collectionViewLayout = layout
我认为 Collection View 的实现绝对可以简化,但我将进一步研究它,因为我可以看到它有多么强大。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)