2016-08-16 6 views
2

UICollectionViewに横方向にスクロールしたくないという問題があります。私は間にスクロールすることができる5つの細胞を表示したい。私のcollectionviewがスクロールしないようにしているのは何ですか? Swift UICollectionView水平スクロールが機能しない

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

編集:UICollectionViewは、実際の対話に反応しない、私は "didSelectAtIndexPath" を試してみました、トリガされません。 UICollectionViewCellでUICollectionViewとUICollectionViewはこのアイデア(collectionViewsの両方がスクロールしている)してみてください実現するために

+0

本当に[ユーザー対話が有効になっていますか?] – DeyaEldeen

+0

@DeyaEldeenデフォルトではそうですね。とにかく、私はそれをtrueに設定しますが、ビューはまだスクロールしたくありません。 – Sn1perSkkN

+0

イメージや詳細を提供してください。とにかく、デバッグしてみましょう... 1-コレクションビューの上に作成されたオブジェクトがあり、その代わりにタッチしますか? 2 - collectionViewの背景色を変更して、フレームがまだ初期化されていないことを確認してください。3 - クラスのタイプがUIViewControllerではないUICollectionViewCellである理由 - 作成終了時にコレクションビューのフレームを印刷できますか?ロジックは、あなたの問題を追跡するのが少し難しいです。 – DeyaEldeen

答えて

0

問題が見つかりました。

親ビューでは、cellForItemAtIndexPath()内のこのビュー(親ビューのUICollectionViewCell)に境界線を追加し、最初のセルのみを読み込み、相互作用を拒否しました。

"child view"内のinit()内に境界線を追加して修正しました。

ありがとうございました。

1

を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

あなたが何を変えたのか、そしてなぜ、感謝してください。 – DeyaEldeen

+0

申し訳ありませんが、わかりません。コードでSn1perSkkNエラーがどこにあるのかコメントしてください。それとも、なぜ私の答えを編集するのですか? –

+0

@VasilyBodnarchuk実際のクラス "featuredCell"はUICollectionViewCellで、 "featuredVideos"内にUICollectionViewを追加したいと思います。 私は最初に新しいクラスを作成し、あなたのコードを使用しました。私はそれを機能させるためにUICollectionViewCellから継承しましたが、それはできませんでした。 それから、私のコードを置き換えました(featuredVideosを初期化しています...)。しかしそれでも問題は解決しません。 「featuredVideos」にスクロールすることを拒否:( – Sn1perSkkN

関連する問題