2016-05-20 99 views
2

我嘗試做一個UICollectionView,在屏幕上打印1,5個單元格,並且使用滾動條應該可以顯示下一個單元格。 但是,這是問題,我設法顯示1.5單元格,但我不能滾動。 我不知道爲什麼,當我將佈局scrollDirection屬性設置爲.Horizontal時,會創建垂直滾動。UICollectionView Horizo​​ntal Scroll

這裏的佈局和UICollectionView初始化:

let screenSize = UIScreen.mainScreen().bounds 
let widthImage = (screenSize.width - CGFloat(8))/1.5 
let heightImage = UIUtils.getHeightAspectRatio(widthImage) 
//let sizeCollectionView: CGSize = CGSize(width: widthImage, height: heightImage) 

let layout = UICollectionViewFlowLayout() 
layout.itemSize = CGSize(width: widthImage, height: heightImage) 

howTo = UICollectionView(frame: self.view.bounds, collectionViewLayout: layout) 
howTo!.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: "howToCell") 
howTo!.tag = 0 
howTo!.delegate = self 
howTo!.dataSource = self 
self.view.addSubview(howTo!) 
howTo!.snp_makeConstraints { (make) -> Void in 
    make.top.equalTo(readMoreText.snp_bottom).offset(5) 
    make.leading.equalTo(self.view).offset(5) 
    make.trailing.equalTo(self.view) 
    make.height.equalTo(115) 
} 
howTo?.backgroundColor = UIColor.whiteColor() 
howTo?.contentMode = .ScaleToFill 
howTo?.scrollEnabled = true 

,這裏是當你顯示單元調用的方法:

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 
if collectionView.tag == howTo!.tag { 
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier("howToCell", forIndexPath: indexPath) 
    if let url = NSURL(string:UIUtils.getImageUrl(self.exercise.getHowTos()[indexPath.row].getImages(), label: Image.mediumWide)) { 
    let screenSize = UIScreen.mainScreen().bounds 
    let widthImage = (screenSize.width - CGFloat(8))/1.5 
    let heightImage = UIUtils.getHeightAspectRatio(widthImage) 
    let img = UIImageView(frame: CGRect(x: 0, y: 0, width: widthImage, height: heightImage)) 
    img.af_setImageWithURL(url) 
    cell.contentView.addSubview(img) 
    cell.backgroundView = cell.contentView 
    } else { 
    let img = UIImageView(frame: cell.frame) 
    img.image = R.image.appIconTemp() 
    cell.contentView.addSubview(img) 
    cell.backgroundView = cell.contentView 
    //cell.backgroundColor = UIColor.blackColor() 
    } 

如果您有任何想法,爲什麼我不能滾動你會讓我的一天過得很愉快!

感謝

回答

7

試試這個

let layout = UICollectionViewFlowLayout() 

layout.scrollDirection = .Vertical 

let collectionView = UICollectionView(frame: frame, collectionViewLayout: layout) 

只是改變滾動方向根據您的需要

+0

THX的答案,但我已經嘗試。 這並沒有改變任何東西,我不能滾動到下一個單元格,我只能打印第一個1和第二個1。 如果我試圖滾動打印第一個單元格的一半和整個第二個 我花了所有昨天嘗試找到問題,但我沒有 –

+0

.horizo​​ntal救了我!謝謝!! –

+0

高興來幫忙@NupurSharma –

相關問題