2016-08-16 5 views
2

Ich habe ein Problem mit einer UICollectionView, die nicht horizontal scrollen möchte. Ich möchte 5 Zellen zeigen, zwischen denen ich scrollen kann. Was verhindert das Scrollen von collectionview?Swift UICollectionView horizontale Scroll funktioniert nicht

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 reagiert eigentlich nicht auf eine Interaktion, habe ich versucht, "didSelectAtIndexPath", löst nicht.

+0

Haben Sie [Benutzerinteraktion aktiviert] zu true? – DeyaEldeen

+0

@DeyaEldeen es ist ja standardmäßig nicht wahr? Wie auch immer, ich setze es auf True, aber die Ansicht will immer noch nicht scrollen. – Sn1perSkkN

+0

Bitte stellen Sie Bilder oder mehr Informationen zur Verfügung, lassen Sie uns debuggen ... 1- Haben Sie ein Objekt, das über der Sammelansicht erstellt wird, die stattdessen die Berührung übernimmt? 2- Bitte ändern Sie die Hintergrundfarbe der collectionView, um sicherzustellen, dass ihr Frame nicht gleich Null ist, wie initialisiert, 3- warum der Klassentyp UICollectionViewCell nicht UIViewController ist, 4- können Sie den Frame der Sammlungsansicht am Ende der Erstellung drucken Logik, es ist ein bisschen schwierig, Ihr Problem zu verfolgen. – DeyaEldeen

Antwort

0

Ich habe das Problem gefunden.

In der übergeordneten Ansicht habe ich dieser Ansicht (in der übergeordneten Ansicht eine UICollectionViewCell) innerhalb der cellForItemAtIndexPath() einen Rahmen hinzugefügt, der dazu führte, dass die Ansicht nur die ersten Zellen geladen und keine Interaktion abgelehnt wurde.

Ich reparierte es, indem ich den Rand in der init() innerhalb der "Kindansicht" hinzufügte, die gerade gut funktionierte.

Vielen Dank für Ihre Hilfe :)

1

Um UICollectionView mit UICollectionView in UICollectionViewCell Realisierung dieser Idee versuchen (beide der collectionViews scrollbaren sind):

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 
} 

} 
+0

Bitte kommentieren Sie mit was Sie geändert haben und warum, danke. – DeyaEldeen

+0

Sorry, aber ich kann es nicht verstehen. Sollte ich kommentieren, wo Sn1perSkkN Fehler im Code waren? Oder, warum bearbeite ich meine Antwort? –

+0

@VasilyBodnarchuk Die eigentliche Klasse "featuredCell" ist eine UICollectionViewCell und ich möchte eine UICollectionView darin "featuredVideos" hinzufügen. Also habe ich zuerst eine neue Klasse erstellt und Ihren Code verwendet, den ich von der UICollectionViewCell geerbt habe, damit es funktioniert, aber es hat nicht funktioniert. Ich habe dann meinen Code ersetzt (Initialisierung featuredVideos ...), aber es löst immer noch nicht das Problem. Die "featuredVideos" weigert sich zu scrollen :( – Sn1perSkkN