Easily embed any ViewController in a Collection/Table View Cell.
To take advantage of this library there are a few steps that you need to follow:
Start by creating a cell that extends from ViewControllerCollectionCell and parameterize it with the ViewController that you intend to present.
class MyCollectionCell: ViewControllerCollectionCell<ViewController> {
override func instantiateViewController() -> ViewController? {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("ViewC") as! ViewController
return vc
}
}
Then register your cell.
override func viewDidLoad() {
super.viewDidLoad()
// Register cell classes
self.collectionView!.registerClass(MyCollectionCell.self, forCellWithReuseIdentifier: reuseIdentifier)
}
And set the current view controller as parent view controller.
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath)
if let cell = cell as? MyCollectionCell {
cell.parentViewController = self
cell.viewController?.model = model[indexPath.item]
}
return cell
}
As the main purpose of this library is to be used with UICollectionViewControllers or UITableViewControllers it provides already some base classes like ViewControllerCollectionCell
and ViewControllerTableCell
. But it can be used to bind any view to any view controller by making the desired view implement the BindableView
protocol.
To run the example project, clone the repo, and run pod install
from the Example directory first.
BindViewControllerToView is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod "BindViewControllerToView"
Francisco Gonçalves, @franciscocsg
BindViewControllerToView is available under the MIT license. See the LICENSE file for more info.