[英]CollectionView produces an error on iPhone SE simulator but not on iPhone 11 simulator
I am a noob to SWIFT but I am trying to create a CollectionView.我是 SWIFT 的菜鸟,但我正在尝试创建一个 CollectionView。 When I run the following code on an iPhone 11 simulator the CollectionView gives me three images per row, which is the result that I want.
当我在 iPhone 11 模拟器上运行以下代码时,CollectionView 每行给我三个图像,这是我想要的结果。 However, when I run the code on any other device I get what seems like a blank cell between the first and third cell on each row.
但是,当我在任何其他设备上运行代码时,我会在每行的第一个和第三个单元格之间看到一个空白单元格。 The image below shows the difference between the working version (11 max pro) and the flawed version (SE).
下图显示了工作版本(11 max pro)和有缺陷的版本(SE)之间的区别。 I am not sure what I am doing wrong or how to correct it.
我不确定我做错了什么或如何纠正它。 I am open to any suggestions on how to fix it or a better way to write the code.
我愿意接受有关如何修复它或编写代码的更好方法的任何建议。
var collectionViewFlowLayout: UICollectionViewFlowLayout!
let cellIdentifier = "ItemCollectionViewCell"
override func viewDidLoad() {
super.viewDidLoad()
setupCollectionView()
}
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
setupCollectionViewItemSize()
}
private func setupCollectionView(){
collectionView.delegate = self
collectionView.dataSource = self
let nib = UINib(nibName: "ItemCollectionViewCell", bundle: nil)
collectionView.register(nib, forCellWithReuseIdentifier: cellIdentifier)
}
private func setupCollectionViewItemSize(){
if collectionViewFlowLayout == nil {
let numberOfItemsPerRow: CGFloat = 3
let lineSpacing: CGFloat = 5
let interItemSpacing: CGFloat = 5
let width = (collectionView.frame.width - (numberOfItemsPerRow - 1) * interItemSpacing) / numberOfItemsPerRow
let height = width
collectionViewFlowLayout = UICollectionViewFlowLayout()
collectionViewFlowLayout.itemSize = CGSize(width: width, height: height)
collectionViewFlowLayout.sectionInset = UIEdgeInsets.zero
collectionViewFlowLayout.scrollDirection = .vertical
collectionViewFlowLayout.minimumLineSpacing = lineSpacing
collectionViewFlowLayout.minimumInteritemSpacing = interItemSpacing
collectionView.setCollectionViewLayout(collectionViewFlowLayout, animated: true)
}
}
I was able to solve it by adding UICollectionViewDelegateFlowLayout to the view controller as well as a function that I found on SO .我能够通过将 UICollectionViewDelegateFlowLayout 添加到视图 controller 以及我在 SO 上找到的 function 来解决它。
extension ViewController: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return items.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellIdentifier, for: indexPath) as! ItemCollectionViewCell
cell.imageView.image = UIImage(named: items[indexPath.item].imageName)
return cell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let item = items[indexPath.item]
performSegue(withIdentifier: viewImageSegueIdentifier, sender: item)
}
//****This is the function that I added ****//
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let noOfCellsInRow = 3
let flowLayout = collectionViewLayout as! UICollectionViewFlowLayout
let totalSpace = flowLayout.sectionInset.left
+ flowLayout.sectionInset.right
+ (flowLayout.minimumInteritemSpacing * CGFloat(noOfCellsInRow - 1))
let size = Int((collectionView.bounds.width - totalSpace) / CGFloat(noOfCellsInRow))
return CGSize(width: size, height: size)
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.