我试图理解为什么集合视图仅将集合中的最后一个单元格居中对齐。
我创建了一个基于流布局的简单集合视图。 我正在使用自动布局标志-我不确定是否会导致此问题。
我创建了一个基于流布局的简单集合视图。 我正在使用自动布局标志-我不确定是否会导致此问题。
每当我从集合视图中删除一个单元格时,前几个似乎运作良好并向左滚动。 但是,当我删除倒数第二个时,最后一个单元格突然从左对齐变为中心对齐。 有人有解释吗? 看起来很奇怪。
如何使所有单元格都向左滚动并保持左对齐。
编辑:这是视图层次结构调试:
http://imgur.com/a/NxidO这是视图行为
我制作了一个演示它的简单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://istack.dev59.com/bledY.gif
// see here for the view hierachy debuggins screen: http://imgur.com/a/NxidO
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])
}
layout.estimatedItemSize = CGSize(width: 1, height: 1)
。 - mn1