2013-07-18 68 views

回答

11

an article that Ash Furrow wrote,它解釋瞭如何將UICollectionView放在UITableViewCell的內部。在UICollectionViewCell中使用它基本上是一樣的想法。

1

這對這個答案來說太晚了,但它可能會幫助其他人。這是UICollectionViewCell內的UICollectionView的示例。

讓我們開始有一個mainCollectionView。然後在這個集合中的每個小區創建和初始化一個新的UICollectionView,合適的地點做的就是在這下面UICollectionView

的代表

func collectionView(collectionView: UICollectionView, willDisplayCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath)

例如,我在這裏初始化MainCollectionViewCell然後MainCollectionViewCell處理,以創建一個邏輯新UICollectionView

guard let collectionViewCell = cell as? MainCollectionViewCell else { return } 

    collectionViewCell.delegate = self 

    let dataProvider = ChildCollectionViewDataSource() 
    dataProvider.data = data[indexPath.row] as NSArray 

    let delegate = ChildCollectionViewDelegate() 

    collectionViewCell.initializeCollectionViewWithDataSource(dataProvider, delegate: delegate, forRow: indexPath.row) 

    collectionViewCell.collectionViewOffset = storedOffsets[indexPath.row] ?? 0 

下面是創建一個新的UICollectionView

MainCollectionViewCell初始化
func initializeCollectionViewWithDataSource<D: protocol<UICollectionViewDataSource>,E: protocol<UICollectionViewDelegate>>(dataSource: D, delegate :E, forRow row: Int) { 

    self.collectionViewDataSource = dataSource 

    self.collectionViewDelegate = delegate 

    let flowLayout = UICollectionViewFlowLayout() 
    flowLayout.scrollDirection = .Horizontal 

    let collectionView = UICollectionView(frame: self.bounds, collectionViewLayout: flowLayout) 
    collectionView.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: reuseChildCollectionViewCellIdentifier) 
    collectionView.backgroundColor = UIColor.whiteColor() 
    collectionView.dataSource = self.collectionViewDataSource 
    collectionView.delegate = self.collectionViewDelegate 
    collectionView.tag = row 

    self.addSubview(collectionView) 

    self.collectionView = collectionView 

    collectionView.reloadData() 
} 

希望幫助!!

我爲此做了一個例子,並放在github上。它演示了在UICollectionViewCell內使用UICollectionView

https://github.com/irfanlone/Collection-View-in-a-collection-view-cell

2

一切程序來完成。沒有故事板。

我在我的UICollectionViewCell中添加了一個UICollectionView。我還展示瞭如何再次添加UICollectionViewCell創建UICollectionView裏面有這樣的結果

enter image description here

import UIKit 

class CategoryCell: UICollectionViewCell, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout { 

    private let cellId = "cell" 

    override init(frame: CGRect) { 
     super.init(frame: frame) 
     setupViews() 
    } 

    required init?(coder aDecoder: NSCoder) { 
     fatalError("init(coder:) has not been implemented") 
    } 

    let appsCollectionView: UICollectionView = { 
     let layout = UICollectionViewFlowLayout() 
     let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout) 
     return collectionView 
    }() 

    func setupViews() { 
     backgroundColor = .blue 

     addSubview(appsCollectionView) 

     appsCollectionView.delegate = self 
     appsCollectionView.dataSource = self 
     appsCollectionView.register(AppCell.self, forCellWithReuseIdentifier: cellId) 

     addConstrainstWithFormat("H:|-8-[v0]-8-|", views: appsCollectionView) 
     addConstrainstWithFormat("V:|[v0]|", views: appsCollectionView) 

    } 

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
     return 5 
    } 

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
     let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) 
     return cell 
    } 
} 

class AppCell: UICollectionViewCell { 
    override init(frame: CGRect) { 
     super.init(frame: frame) 
     setupViews() 
    } 

    required init?(coder aDecoder: NSCoder) { 
     fatalError("init(coder:) has not been implemented") 
    } 

    func setupViews(){ 
     backgroundColor = .red 
    } 
} 

我UICollectionViewController

import UIKit 

class FeaturedAppsController: UICollectionViewController, UICollectionViewDelegateFlowLayout { 

    let cellId = "cell" 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     // Do any additional setup after loading the view, typically from a nib. 

     collectionView?.backgroundColor = .white 
     collectionView?.register(CategoryCell.self, forCellWithReuseIdentifier: cellId) 
    } 

    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
     return 3 
    } 

    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
     let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) 
     return cell 
    } 

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { 
     return CGSize(view.frame.width, 150) 
    } 

} 

所有的解釋可以發現,被​​開發的「讓的構建該應用程序「:https://www.youtube.com/watch?v=Ko9oNhlTwH0&list=PL0dzCUj1L5JEXct3-OV6itP7Kz3tRDmma

相關問題