gpt4 book ai didi

ios - UICollectionView 重叠单元格

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

我有一个以编程方式创建和添加的 UICollectionView,但由于某种原因,在运行时第 1 部分和第 2 部分重叠,它们都从同一位置开始:

这是我的实现(代码是 swift 2.3,因为这是项目要求):

  override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = lightGreyColor
layout.sectionInset = UIEdgeInsets(top: 0, left: 0, bottom: 15, right: 0)
layout.minimumInteritemSpacing = 0.5
layout.minimumLineSpacing = 0.5
layout.scrollDirection = UICollectionViewScrollDirection.Vertical
// layout.headerReferenceSize = CGSize(width: view.bounds.width, height: 30)

let collectionViewFrame = CGRect(x: 0, y: 0, width: view.bounds.width, height: viewHeight)
collectionView = UICollectionView(frame: collectionViewFrame, collectionViewLayout: layout)

collectionView.allowsSelection = true
collectionView.bounces = true
collectionView.scrollEnabled = true
collectionView.hidden = false
collectionView.pagingEnabled = false
collectionView.backgroundColor = lightGreyColor
collectionView.showsVerticalScrollIndicator = false
collectionView.showsHorizontalScrollIndicator = false

collectionView.delegate = self
collectionView.dataSource = self

view.addSubview(collectionView)

collectionView.registerNib(UINib(nibName: "SmallDiscoverItemCollectionViewCell", bundle: NSBundle.mainBundle()), forCellWithReuseIdentifier: "SmallDiscoverItemCollectionViewCell")

collectionView.registerClass(NewDiscoverTopCollectionViewCell.self, forCellWithReuseIdentifier: "NewDiscoverTopCollectionViewCell")
collectionView.registerClass(TopHeaderCell.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "TopHeaderCell")

greedoCollectionViewLayout = nil
greedoCollectionViewLayout1().rowMaximumHeight = collectionView.bounds.width/1.8
greedoCollectionViewLayout1().fixedHeight = false

}


func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {

switch indexPath.section {
case 0:
return CGSize(width: collectionView.bounds.width, height: 200)
case 1:
return CGSize(width: collectionView.bounds.width, height: 200)
default:
return self.greedoCollectionViewLayout1().sizeForPhotoAtIndexPath(indexPath)
}

}
}

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
switch (indexPath.section) {
case 0: let cell = collectionView.dequeueReusableCellWithReuseIdentifier("NewDiscoverTopCollectionViewCell", forIndexPath: indexPath) as! NewDiscoverTopCollectionViewCell

return cell

case 1: let cell = collectionView.dequeueReusableCellWithReuseIdentifier("NewDiscoverTopCollectionViewCell", forIndexPath: indexPath) as! NewDiscoverTopCollectionViewCell

return cell

case 2: let cell = collectionView.dequeueReusableCellWithReuseIdentifier("SmallDiscoverItemCollectionViewCell", forIndexPath: indexPath) as! SmallDiscoverItemCollectionViewCell
let item = usersItems[indexPath.item]
return cell
}

这是重叠的最终结果:第 0 部分和第 1 部分具有绿色背景,而中间有间隙。虽然第 2 部分有很多图像:

enter image description here

最佳答案

出于某种奇怪的原因,UICollectionViewCell 被初始化为等于其大小的 y 位置。为了解决这个问题,我刚刚将其添加到 UICollectionViewCell

override init(frame: CGRect) {
let noProblemFrame = CGRect(x: 0, y: 0, width: frame.width, height: frame.height)
super.init(frame: noProblemFrame)

关于ios - UICollectionView 重叠单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42071930/

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