Swift 正在将额外单元格添加到UICollection视图的最后一行
这是我的UICollectionView代码:Swift 正在将额外单元格添加到UICollection视图的最后一行,swift,uicollectionview,uicollectionviewcell,uicollectionviewlayout,Swift,Uicollectionview,Uicollectionviewcell,Uicollectionviewlayout,这是我的UICollectionView代码: extension UserFeed: UICollectionViewDelegate, UICollectionViewDataSource { func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { resizeCollectionView() return fee
extension UserFeed: UICollectionViewDelegate, UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
resizeCollectionView()
return feeds.count
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
userFeedDelegate?.didSelectFeed(feeds[indexPath.row])
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: UserFeedCollectionViewCell.kCellIdentifier, for: indexPath) as! UserFeedCollectionViewCell
print(indexPath.row)
cell.setup(feeds[indexPath.row])
return cell
}
}
下面是“我的收藏”视图的设置
feedLayout.sectionInset = UIEdgeInsets.zero
feedLayout.itemSize = CGSize(width: size, height: size)
feedLayout.scrollDirection = .vertical
feedLayout.minimumInteritemSpacing = 0
feedLayout.minimumLineSpacing = 0
在本例中,feeds.count为2,由打印的indexath.row数量创建的单元格为2:0,1
但是,在输出中,将显示4个单元格以及自定义UICollectionView单元格类型的最后一个未实例化版本。如果有帮助的话,我只是在视图中添加滚动后才看到这些额外的单元格
谢谢
编辑:
“调整收藏大小”视图功能:
fileprivate func resizeCollectionView() {
let height:CGFloat = CGFloat(Int(ceil(Double(feeds.count)/2))) * size
frame = CGRect(x: 0, y: frame.origin.y, width: frame.width, height: height)
userFeedDelegate?.didChangedCollectionHeight()
}
resizeCollectionView有问题,请尝试在cellForItemAt IndExpath中执行此操作。我尝试了此操作,但现在它根本不加载任何内容。我还添加了调整集合大小的功能。您是否将
numberOfSections(在collectionView:UICollectionView中)
设置为1
?