Created
January 16, 2019 16:37
-
-
Save theangelperalta/8b2c39effc4681a53991d5ea46a8ed30 to your computer and use it in GitHub Desktop.
GenericCollectionViewController
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Created by Angel Cortez on 3/30/18. | |
// Copyright © 2018 Angel Cortez. All rights reserved. | |
// | |
// This an adaptation of Brian Voong's GenericTableViewController | |
import UIKit | |
class GenericCell<U>: UITableViewCell { | |
var item: U! | |
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) { | |
super.init(style: style, reuseIdentifier: reuseIdentifier) | |
setupViews() | |
} | |
func setupViews() {} | |
required init?(coder aDecoder: NSCoder) { | |
fatalError("init(coder:) has not been implemented") | |
} | |
} | |
class GenericTableViewController<T: GenericCell<U>, U>: UITableViewController { | |
var items = [U]() | |
func setupTableView(){} | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
let reuseIdentifier = NSStringFromClass(T.self) | |
tableView.register(T.self, forCellReuseIdentifier: reuseIdentifier) | |
setupTableView() | |
} | |
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { | |
return 1 | |
} | |
override func numberOfSections(in tableView: UITableView) -> Int { | |
return items.count | |
} | |
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { | |
let identifier = NSStringFromClass(T.self) | |
let cell = tableView.dequeueReusableCell(withIdentifier: identifier, for: indexPath) as! T | |
cell.item = items[indexPath.section] | |
return cell | |
} | |
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? { | |
return super.tableView(tableView, titleForHeaderInSection: section) | |
} | |
} | |
class GenericViewController<T: GenericCell<U>, U>: UIViewController, UITableViewDelegate, UITableViewDataSource{ | |
var items = [U]() { | |
didSet{ | |
DispatchQueue.main.async { | |
self.tableView.reloadData() | |
} | |
} | |
} | |
var tableView = UITableView() | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
tableView.delegate = self | |
tableView.dataSource = self | |
let reuseIdentifier = NSStringFromClass(T.self) | |
tableView.register(T.self, forCellReuseIdentifier: reuseIdentifier) | |
} | |
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { | |
return items.count | |
} | |
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { | |
let identifier = NSStringFromClass(T.self) | |
let cell = tableView.dequeueReusableCell(withIdentifier: identifier, for: indexPath) as! T | |
cell.item = items[indexPath.row] | |
return cell | |
} | |
func scrollViewWillBeginDragging(_ scrollView: UIScrollView) { | |
} | |
func scrollViewDidScroll(_ scrollView: UIScrollView) { | |
} | |
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) { | |
} | |
func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) { | |
} | |
} | |
class GenericCollectionViewCell<U>: UICollectionViewCell { | |
var item: U! | |
override init(frame: CGRect) { | |
super.init(frame: frame) | |
setupViews() | |
} | |
func setupViews() {} | |
required init?(coder aDecoder: NSCoder) { | |
fatalError("init(coder:) has not been implemented") | |
} | |
} | |
class GenericCollectionView<T: GenericCollectionViewCell<U>, U>: UIView, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout, UIGestureRecognizerDelegate { | |
var items = [U]() | |
{ | |
didSet{ | |
DispatchQueue.main.async { | |
self.collectionView.reloadData() | |
} | |
} | |
} | |
var numberOfSections: Int? | |
let collectionView = UICollectionView(frame: CGRect(x: 0, y: 0, width: 0, height: 0), collectionViewLayout: UICollectionViewFlowLayout()) | |
override init(frame: CGRect) { | |
super.init(frame: frame) | |
collectionView.dataSource = self | |
collectionView.delegate = self | |
self.addSubview(collectionView) | |
setupCollectionView() | |
let reuseIdentifer = NSStringFromClass(T.self) | |
collectionView.register(T.self, forCellWithReuseIdentifier: reuseIdentifer) | |
} | |
required init?(coder aDecoder: NSCoder) { | |
fatalError("init(coder:) has not been implemented") | |
} | |
func setupCollectionView() {} | |
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { | |
return items.count | |
} | |
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { | |
let identifer = NSStringFromClass(T.self) | |
var cell = collectionView.dequeueReusableCell(withReuseIdentifier: identifer, for: indexPath) as! T | |
cell.item = items[indexPath.row] | |
// if cell.gestureRecognizers?.count == nil { | |
// let tap = UITapGestureRecognizer(target: self, action: #selector(cellTapped)) | |
// tap.allowedPressTypes = [NSNumber(value: UIPressType.select.rawValue)] | |
// cell.addGestureRecognizer(tap) | |
// } | |
return cell | |
} | |
// @objc func cellTapped(gesture: UITapGestureRecognizer) {} | |
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { | |
} | |
func numberOfSections(in collectionView: UICollectionView) -> Int { | |
return (numberOfSections == nil) ? 1: numberOfSections! | |
} | |
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { | |
return CGSize(width: 385, height: 601) | |
} | |
} | |
class GenericCollectionViewController<T: GenericCollectionViewCell<U>, U>: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout, UIGestureRecognizerDelegate { | |
var items = [[U]](){ | |
didSet{ | |
DispatchQueue.main.async { | |
self.collectionView.reloadData() | |
} | |
} | |
} | |
var numberOfSections: Int? | |
let collectionView = UICollectionView(frame: CGRect(x: 0, y: 0, width: 0, height: 0), collectionViewLayout: UICollectionViewFlowLayout()) | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
collectionView.dataSource = self | |
collectionView.delegate = self | |
view.addSubview(collectionView) | |
setupCollectionView() | |
let reuseIdentifer = NSStringFromClass(T.self) | |
collectionView.register(T.self, forCellWithReuseIdentifier: reuseIdentifer) | |
} | |
func setupCollectionView() {} | |
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { | |
return (section < items.count) ? items[section].count : 0 | |
} | |
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { | |
let identifer = NSStringFromClass(T.self) | |
var cell = collectionView.dequeueReusableCell(withReuseIdentifier: identifer, for: indexPath) as! T | |
cell.item = items[indexPath.section][indexPath.row] | |
if cell.gestureRecognizers?.count == nil { | |
let tap = UITapGestureRecognizer(target: self, action: #selector(cellTapped)) | |
tap.allowedPressTypes = [NSNumber(value: UIPress.PressType.select.rawValue)] | |
cell.addGestureRecognizer(tap) | |
} | |
return cell | |
} | |
@objc func cellTapped(gesture: UITapGestureRecognizer) {} | |
func numberOfSections(in collectionView: UICollectionView) -> Int { | |
return (numberOfSections == nil) ? 1: numberOfSections! | |
} | |
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { | |
return CGSize(width: 385, height: 601) | |
} | |
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets { | |
return UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment