franciscocgoncalves / BindViewControllerToView

Easily embed any ViewController and its respective ViewModel in a Collection/Table View Cell.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

BindViewControllerToView

Version License Platform

Easily embed any ViewController in a Collection/Table View Cell.

Basic Usage

To take advantage of this library there are a few steps that you need to follow:

CollectionViewCell

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
  }
}

CollectionViewController

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
}

Customization

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.

Example

To run the example project, clone the repo, and run pod install from the Example directory first.

Installation

BindViewControllerToView is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod "BindViewControllerToView"

Author

Francisco Gonçalves, @franciscocsg

License

BindViewControllerToView is available under the MIT license. See the LICENSE file for more info.

About

Easily embed any ViewController and its respective ViewModel in a Collection/Table View Cell.

License:MIT License


Languages

Language:Shell 56.8%Language:Swift 31.7%Language:Ruby 7.8%Language:Objective-C 3.7%