Swift UICollectionView水平滚动不起作用

时间:2022-09-06 20:47:54

I have an issue with a UICollectionView that doesn't want to scroll horizontally. I want to show 5 cells that I can scroll between. What is preventing my collectionview from scrolling?

我有一个不想水平滚动的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
}
}

Swift UICollectionView水平滚动不起作用

Edit : UICollectionView actually doesn't react to any interaction, i tried "didSelectAtIndexPath", doesn't trigger.

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

2 个解决方案

#1


1  

To realize UICollectionView with UICollectionView in UICollectionViewCell try this idea (both of the collectionViews are scrollable):

要在UICollectionViewCell中使用UICollectionView实现UICollectionView,请尝试这个想法(两个collectionView都是可滚动的):

CollectionViewController.swift

CollectionViewController.swift

import UIKit

class CollectionViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout
{
var featuredVideos: UICollectionView?

override func viewDidLoad() {

    let layout = UICollectionViewFlowLayout()
    layout.scrollDirection = .Horizontal
    featuredVideos = UICollectionView(frame: UIScreen.mainScreen().bounds, 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(CollectionViewCell.self, forCellWithReuseIdentifier: "cellId")
    view.addSubview(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) as! CollectionViewCell
    cell.initCell()
    if indexPath.item%2 == 0
    {
        cell.backgroundColor = .lightGrayColor()
    }
    else
    {
        cell.backgroundColor = .brownColor()
    }
    return cell
}

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize
{
    return CGSizeMake(300, UIScreen.mainScreen().bounds.height)
}

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

CollectionViewCell.swift

CollectionViewCell.swift

class CollectionViewCell: UICollectionViewCell, UICollectionViewDataSource, UICollectionViewDelegate
{
var collectionView: UICollectionView?

func initCell () {
    let layout = UICollectionViewFlowLayout()
    layout.scrollDirection = .Horizontal
    var collectionViewBounds = self.bounds
    collectionViewBounds.size.height -= 80
    collectionViewBounds.origin.y = 40
    collectionView = UICollectionView(frame: collectionViewBounds, collectionViewLayout: layout)

    collectionView!.dataSource = self
    collectionView!.delegate = self

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

    collectionView!.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: "cellWithCollectionView")
    addSubview(collectionView!)

}

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

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell
{
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cellWithCollectionView", forIndexPath: indexPath)
    if indexPath.item%2 == 0
    {
        cell.backgroundColor = .blueColor()
    }
    else
    {
        cell.backgroundColor = .whiteColor()
    }
    return cell
}

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

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

}

#2


1  

I've found the problem.

我发现了问题。

In the parent view, I added a border to this view (which is a UICollectionViewCell in the parent view) inside the cellForItemAtIndexPath(), and that caused the view to only load the first cells and refuse any interaction.

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

I fixed it by adding the border in the init() inside the "child view" which worked just fine.

我通过在“子视图”中的init()中添加边框来修复它,它工作正常。

Thank you all for your help :)

感谢大家的帮助 :)

#1


1  

To realize UICollectionView with UICollectionView in UICollectionViewCell try this idea (both of the collectionViews are scrollable):

要在UICollectionViewCell中使用UICollectionView实现UICollectionView,请尝试这个想法(两个collectionView都是可滚动的):

CollectionViewController.swift

CollectionViewController.swift

import UIKit

class CollectionViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout
{
var featuredVideos: UICollectionView?

override func viewDidLoad() {

    let layout = UICollectionViewFlowLayout()
    layout.scrollDirection = .Horizontal
    featuredVideos = UICollectionView(frame: UIScreen.mainScreen().bounds, 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(CollectionViewCell.self, forCellWithReuseIdentifier: "cellId")
    view.addSubview(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) as! CollectionViewCell
    cell.initCell()
    if indexPath.item%2 == 0
    {
        cell.backgroundColor = .lightGrayColor()
    }
    else
    {
        cell.backgroundColor = .brownColor()
    }
    return cell
}

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize
{
    return CGSizeMake(300, UIScreen.mainScreen().bounds.height)
}

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

CollectionViewCell.swift

CollectionViewCell.swift

class CollectionViewCell: UICollectionViewCell, UICollectionViewDataSource, UICollectionViewDelegate
{
var collectionView: UICollectionView?

func initCell () {
    let layout = UICollectionViewFlowLayout()
    layout.scrollDirection = .Horizontal
    var collectionViewBounds = self.bounds
    collectionViewBounds.size.height -= 80
    collectionViewBounds.origin.y = 40
    collectionView = UICollectionView(frame: collectionViewBounds, collectionViewLayout: layout)

    collectionView!.dataSource = self
    collectionView!.delegate = self

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

    collectionView!.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: "cellWithCollectionView")
    addSubview(collectionView!)

}

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

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell
{
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cellWithCollectionView", forIndexPath: indexPath)
    if indexPath.item%2 == 0
    {
        cell.backgroundColor = .blueColor()
    }
    else
    {
        cell.backgroundColor = .whiteColor()
    }
    return cell
}

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

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

}

#2


1  

I've found the problem.

我发现了问题。

In the parent view, I added a border to this view (which is a UICollectionViewCell in the parent view) inside the cellForItemAtIndexPath(), and that caused the view to only load the first cells and refuse any interaction.

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

I fixed it by adding the border in the init() inside the "child view" which worked just fine.

我通过在“子视图”中的init()中添加边框来修复它,它工作正常。

Thank you all for your help :)

感谢大家的帮助 :)