2016-08-16 162 views
2

我遇到了不想水平滾動的UICollectionView問題。我想顯示5個單元格,我可以在它們之間滾動。什麼是阻止我滾動的collectionviewSwift 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」,不會觸發。

+0

你有[啓用用戶交互]爲真? – DeyaEldeen

+0

@DeyaEldeen這是默認的是不是?無論如何,我將它設置爲true,但視圖仍然不想滾動。 – Sn1perSkkN

+0

請提供圖片或更多信息,無論如何,讓我們來調試...... 1-你有一個對象是在集合視圖上面創建的嗎? 2-請更改collectionView的背景顏色以確保它的框架在初始化後不爲零,3-爲什麼類類型是UICollectionViewCell而不是UIViewController,4-是否可以在創建結束時打印集合視圖的框架邏輯,跟蹤你的問題有點困難。 – DeyaEldeen

回答

0

我發現了這個問題。

在父視圖中,我向cellForItemAtIndexPath()中的此視圖(這是父視圖中的UICollectionViewCell)添加了一個邊框,並導致視圖僅加載第一個單元並拒絕任何交互。

我通過在「子視圖」裏面的init()中添加邊界來修復它,它工作得很好。

謝謝大家的幫助:)

1

要與UICollectionView實現UICollectionView在UICollectionViewCell嘗試這種想法(這兩個collectionViews是滾動):

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,我想在它裏面添加一個UICollectionView「featuredVideos」。 因此,我首先創建了一個新類,並使用您的代碼,我從UICollectionViewCell繼承,以使其工作,但它沒有。 然後,我取代了我的代碼(初始化特色視頻...),但它仍然不能解決問題。 「特色視頻」拒絕滾動:( – Sn1perSkkN