Skip to content

Instantly share code, notes, and snippets.

@bgerstle
Created November 12, 2015 16:41
Show Gist options
  • Save bgerstle/a9c060fc31de9c3bed08 to your computer and use it in GitHub Desktop.
Save bgerstle/a9c060fc31de9c3bed08 to your computer and use it in GitHub Desktop.
Demonstrating protocol extensions on UIViewController.
import UIKit
public protocol CellContainer {
var visibleCells: [NSIndexPath] { get }
}
public protocol CollectionViewContainer {
var collectionView: UICollectionView? { get }
}
extension CellContainer where Self: CollectionViewContainer {
public var visibleCells: [NSIndexPath] {
get {
return self.collectionView?.indexPathsForVisibleItems() ?? []
}
}
}
// seems like these need to be delcared explicitly?
extension UICollectionViewController: CollectionViewContainer { }
// without this, compiler complains that UICollectionViewController has no member "visibleCells"
extension UICollectionViewController: CellContainer { }
let collectionVC = UICollectionViewController(collectionViewLayout: UICollectionViewFlowLayout())
collectionVC.visibleCells
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment