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
}
}
Edit: UICollectionView reagiert eigentlich nicht auf eine Interaktion, habe ich versucht, "didSelectAtIndexPath", löst nicht.
Haben Sie [Benutzerinteraktion aktiviert] zu true? – DeyaEldeen
@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
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