Sn1perSkkN
Sn1perSkkN

Reputation: 113

Swift UICollectionView horizontal scroll not working

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?

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
}
}

simulator view

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

Upvotes: 5

Views: 14958

Answers (3)

DHARMENDRA SINHA
DHARMENDRA SINHA

Reputation: 1

If you are using Scroll View delegate methods, then this problem may come.

So, resolve by adding this line into those delegate methods :

func scrollViewDidScroll(_ scrollView: UIScrollView) {        
    if scrollView.isKind(of: UICollectionView.self) 
      {
         return
      };
}

Upvotes: 0

Sn1perSkkN
Sn1perSkkN

Reputation: 113

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.

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

Thank you all for your help :)

Upvotes: 1

Vasily  Bodnarchuk
Vasily Bodnarchuk

Reputation: 25294

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

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

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
}

}

Upvotes: 1

Related Questions