Swift UICollectionView 水平滚动不起作用

Sn1*_*kkN 5 iphone scroll ios uicollectionview swift

UICollectionView我有一个不想水平滚动的问题。我想显示 5 个可以滚动的单元格。是什么阻止我collectionview滚动?

import UIKit

class FeaturedCell: UICollectionViewCell, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout 
{
// Attributes
lazy var featuredVideos = UICollectionView(frame: .zero)

// Superclass initializer
required init?(coder aDecoder: NSCoder)
{
    fatalError("init(coder:) has not been implemented")
}

// Custom initializer
required override init(frame: CGRect)
{
    super.init(frame: frame)

    let layout = UICollectionViewFlowLayout()
    layout.scrollDirection = .Horizontal
    featuredVideos = UICollectionView(frame: self.frame, collectionViewLayout: layout)
    featuredVideos.dataSource = self
    featuredVideos.delegate = self

    // Setting the collection view's scrolling behaviour
    featuredVideos.pagingEnabled = true
    featuredVideos.scrollEnabled = true
    featuredVideos.setContentOffset(CGPoint(x: 0,y: 0), animated: true)

    featuredVideos.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: "cellId")

    addSubview(featuredVideos)
    setConstraints("H:|[v0(\(frame.width))]|", subviews: featuredVideos)
    setConstraints("V:|[v0(345)]", subviews: featuredVideos)
}

func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return 5
}

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell
{
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cellId", forIndexPath: indexPath)
    if indexPath.item == 1 { cell.backgroundColor = .lightGrayColor() } else { cell.backgroundColor = .brownColor() }
    return cell
}

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize
{
    return CGSizeMake(frame.width/3, frame.height)
}

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAtIndex section: Int) -> CGFloat
{
    return 10
}
}
Run Code Online (Sandbox Code Playgroud)

模拟器视图

编辑: UICollectionView 实际上不会对任何交互做出反应,我尝试了“didSelectAtIndexPath”,但没有触发。

Sn1*_*kkN 1

我已经找到问题了。

在父视图中,我在 cellForItemAtIndexPath() 内为此视图(父视图中的 UICollectionViewCell)添加了一个边框,这导致视图仅加载第一个单元格并拒绝任何交互。

我通过在“子视图”内的 init() 中添加边框来修复它,效果很好。

感谢大家的帮助 :)