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
}
}
編集:UICollectionViewは、実際の対話に反応しない、私は "didSelectAtIndexPath" を試してみました、トリガされません。 UICollectionViewCellでUICollectionViewとUICollectionViewはこのアイデア(collectionViewsの両方がスクロールしている)してみてください実現するために
本当に[ユーザー対話が有効になっていますか?] – DeyaEldeen
@DeyaEldeenデフォルトではそうですね。とにかく、私はそれをtrueに設定しますが、ビューはまだスクロールしたくありません。 – Sn1perSkkN
イメージや詳細を提供してください。とにかく、デバッグしてみましょう... 1-コレクションビューの上に作成されたオブジェクトがあり、その代わりにタッチしますか? 2 - collectionViewの背景色を変更して、フレームがまだ初期化されていないことを確認してください。3 - クラスのタイプがUIViewControllerではないUICollectionViewCellである理由 - 作成終了時にコレクションビューのフレームを印刷できますか?ロジックは、あなたの問題を追跡するのが少し難しいです。 – DeyaEldeen