UICollectionView 重叠单元格

UICollectionView overlapping cells

我有一个以编程方式创建和添加的 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 部分有很多图片:

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

中添加了这个
override init(frame: CGRect) {
let noProblemFrame = CGRect(x: 0, y: 0, width: frame.width, height: frame.height)
super.init(frame: noProblemFrame)