cellForItemAt如果numberOfItemsInSection为1 UICollectionView,则不会调用

问题描述 投票:0回答:1

下面是我的代码,它是如果numberOfItemsInSection大于1可以正常工作,但> [,因为numberOfItemsInSection等于1,将显示并注意未调用cellForItemAt在进行api调用并接收数据后,它的初始numberOfItemsInSection也为零,我也调用了以下代码

tabsCollectionView.reloadData()

其他代码

tabsCollectionView.delegate = self tabsCollectionView.dataSource = self func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return menuResult?.foodMenuItems?.count ?? 0 } func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell = tabsCollectionView.dequeueReusableCell(withReuseIdentifier: "foodTabsCollectionViewCellID", for: indexPath) as! FoodTabsCollectionViewCell cell.foodMenuItem = menuResult?.foodMenuItems?[indexPath.row] cell.setUI() return cell } func numberOfSections(in collectionView: UICollectionView) -> Int { return 1 }

以下是与代码相关的侧视图viewDidLoad()

if let flowLayout = tabsCollectionView.collectionViewLayout as? UICollectionViewFlowLayout { flowLayout.estimatedItemSize = CGSize(width: 200, height: 70) flowLayout.itemSize = UICollectionViewFlowLayout.automaticSize tabsCollectionView.collectionViewLayout = flowLayout }

sizeForItemAt方法

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { var height = collectionView.frame.height var width = collectionView.frame.width if let flowLayout = collectionView.collectionViewLayout as? UICollectionViewFlowLayout { width = flowLayout.estimatedItemSize.width height = flowLayout.estimatedItemSize.height } return CGSize(width: width, height: height) }

以下为FoodTabsCollectionViewCell中的代码

override func preferredLayoutAttributesFitting(_ layoutAttributes: UICollectionViewLayoutAttributes) -> UICollectionViewLayoutAttributes { setNeedsLayout() layoutIfNeeded() let size = contentView.systemLayoutSizeFitting(layoutAttributes.size) var frame = layoutAttributes.frame frame.size.width = ceil(size.width) frame.size.height = ceil(size.height) layoutAttributes.frame = frame layoutIfNeeded() return layoutAttributes }
以下是我的代码,如果numberOfItemsInSection大于1,但工作正常,但如果numberOfItemsInSection等于1,则它应该显示单个单元格,但不会发生,对于...
ios swift uicollectionview cell uicollectionviewcell
1个回答
-1
投票
如果为menuResult?.foodMenuItems?.count > 0,但没有任何显示。您能告诉我如何设置FoodTabsCollectionViewCell的大小吗?
© www.soinside.com 2019 - 2024. All rights reserved.