2017-06-20 88 views
0

我看过很多方法在UICollectionView中获得传送带/一行/一节。但非工作可能,你有办法。UICollectionViewFlowLayout一行/横向部分

我有以下几点:

@IBAction func openCollectionView(_ sender: Any) { 
    let layout = UICollectionViewFlowLayout() 
    layout.scrollDirection = .horizontal 
    layout.itemSize = CGSize(width: 50, height: self.view.frame.height) 
    collectionView = UICollectionView(frame: self.view.frame, collectionViewLayout: layout) 

    collectionView!.delegate = self 
    collectionView!.dataSource = self 
    collectionView!.register(PDCollectionViewCell.self, forCellWithReuseIdentifier: "cellIdentifier") 

    collectionView?.contentOffset.x = 20 

    collectionView?.reloadData() 
    collectionView?.layoutIfNeeded() 

    collectionView!.addBlurEffect() 
    self.view.addSubview(collectionView!) 

} 
+1

这是很容易,你只需要将collectionView的布局设置为水平,然后在你的VC的dataSource方法中,为section指定1,为yourIndexArray.count指定itemsInSection –

回答

0

好 - 很简单:

layout.itemSize = CGSize(width: (self.view.frame.size.width/2)+50, height: (self.view.frame.size.height/2)+50) 

而对于更复杂的细胞:

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize { 
    return CGSize(width: 100, height: 100) 
}