gpt4 book ai didi

ios - UICollectionView 水平分页仅在第一页上每行显示 3 个单元格

转载 作者:行者123 更新时间:2023-11-30 11:06:53 25 4
gpt4 key购买 nike

我有一个 UICollectionView,我已经实现了水平滚动,但由于某种原因,当我滚动时,只显示了 3 个单元格(本应有 9 个)

class BusinessPhotosViewController: UICollectionViewController ,UICollectionViewDelegateFlowLayout{

let cellId = "photos"

override func viewDidLoad() {
super.viewDidLoad()

if let layout = collectionView?.collectionViewLayout as? UICollectionViewFlowLayout {
layout.scrollDirection = .horizontal
layout.minimumLineSpacing = 0
}

collectionView?.register(BusinessPhotoCells.self, forCellWithReuseIdentifier: cellId)
collectionView?.contentInset = UIEdgeInsets.init(top: 0, left: 0, bottom: 0, right: 0)
collectionView?.scrollIndicatorInsets = UIEdgeInsets.init(top: 0, left: 0, bottom: 0, right: 0)
collectionView?.isPagingEnabled = true
}

override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 9
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
return 0
}

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! BusinessPhotoCells

return cell
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let width = view.frame.width / 3
return CGSize(width: width, height: width)
}
}

我在另一个名为 BusinessDetailViewController 的 UICollectionViewController 的单元格中呈现此 BusinessPhotosViewController

class BusinessDetailViewController : UICollectionViewController {

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

let scrollCell = collectionView.dequeueReusableCell(withReuseIdentifier: pictureCellId, for: indexPath)

let bizVC = BusinessPhotosViewController(collectionViewLayout: UICollectionViewFlowLayout())

scrollCell.addSubview(bizVC.view)
bizVC.view.anchor(top: scrollCell.topAnchor, left: scrollCell.leftAnchor, bottom: scrollCell.bottomAnchor, right: scrollCell.rightAnchor, paddingTop: 0, paddingLeft: 0, paddingBottom: 0, paddingRight: 0, width: scrollCell.frame.width, height: scrollCell.frame.height)

return scrollCell
}
}

所以我可以看到滚动有效,但我看到的问题是只加载了 3 个单元格(总共 9 个),并且 View 看起来像这样:

enter image description here

我见过这些解决方案herehere和其他但没有帮助我。

最佳答案

我尝试了你的代码,如果 9 个单元格的大小小于滚动单元格的大小,我可以看到所有 9 个单元格。

import UIKit

class ViewController: UIViewController{
override func viewDidLoad() {
view.backgroundColor = .red;

view.addSubview(photosContainer);

photosContainer.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true;
photosContainer.topAnchor.constraint(equalTo: view.centerYAnchor).isActive = true;
photosContainer.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true;
photosContainer.heightAnchor.constraint(equalTo: view.heightAnchor, multiplier: 0.4).isActive = true;

***let layout = UICollectionViewFlowLayout();
layout.scrollDirection = .horizontal
let bizVC = BusinessPhotosViewController(collectionViewLayout: layout)
bizVC.view.frame = photosContainer.bounds;
photosContainer.addSubview(bizVC.view)***

}

let photosContainer : UIView = {
let view = UIView();
view.backgroundColor = .green;
view.translatesAutoresizingMaskIntoConstraints = false;
return view;
}();
}

在添加 View 之前,如果您设置了框架,那么我可以看到所有九个单元格,无论它们的大小。

Screenshot

让我知道它是否有效

关于ios - UICollectionView 水平分页仅在第一页上每行显示 3 个单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52633427/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com