繁体   English   中英

UICollectionView重叠的单元格

[英]UICollectionView overlapping cells

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

这是我的实现(代码是迅速的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初始化到了等于其大小的ay位置。 为了解决这个问题,我刚刚在UICollectionViewCell中添加了它

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM