Skip to content

Instantly share code, notes, and snippets.

@FlorianBasso
Created June 1, 2016 18:29
Show Gist options
  • Save FlorianBasso/cdded21971101c4c7eb7c1a16f2d0384 to your computer and use it in GitHub Desktop.
Save FlorianBasso/cdded21971101c4c7eb7c1a16f2d0384 to your computer and use it in GitHub Desktop.
Example of CellItem protocol implementation
enum MovieType: String {
case Poster = "Poster"
case Cover = "Cover"
case Side = "Side"
static var allValues = [Cover, Poster, Side]
}
struct MovieCellItem {
// MARK: - Properties
let movie: Movie
let type: MovieType
// MARK: - Initialization
init(movie: Movie, type: MovieType) {
self.movie = movie
self.type = type
}
// MARK: - Helper Methods
func nibNameForType(type: MovieType) -> String {
return "Movie\(type.rawValue)Cell"
}
func heightForType(type: MovieType) -> CGFloat {
switch type {
case .Cover:
return 200
default:
return 100
}
}
}
// MARK: - CellItem protocol
extension MovieCellItem: CellItem {
func reuseIdentifier() -> String {
return "\(String(MovieCell))\(self.type.rawValue)"
}
func configureCell(cell: UICollectionViewCell) {
if let movieCell = cell as? MovieCell {
movieCell.titleLabel.text = self.movie.title
movieCell.titleLabel.textColor = UIColor.darkGrayColor()
movieCell.imageView.image = UIImage(named: "\(self.movie.imageName)\(self.type.rawValue)")
}
}
func cellSize(maxSize: CGSize) -> CGSize {
return CGSize(width: maxSize.width, height: self.heightForType(self.type))
}
func cellNib() -> UINib? {
let nibName = self.nibNameForType(self.type)
return UINib(nibName: nibName, bundle: NSBundle.mainBundle())
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment