Created
May 14, 2016 14:08
-
-
Save psaitu/ce44dd4fb60d4ca7f9c2e9179a2eca14 to your computer and use it in GitHub Desktop.
Better tableviews using generics
This file contains hidden or 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
| import UIKit | |
| class BookCell: UITableViewCell { | |
| @IBOutlet weak var redLabel: UILabel! | |
| override func awakeFromNib() { | |
| super.awakeFromNib() | |
| // Initialization code | |
| } | |
| override func setSelected(selected: Bool, animated: Bool) { | |
| super.setSelected(selected, animated: animated) | |
| // Configure the view for the selected state | |
| } | |
| } | |
| extension BookCell:NibLoadableView { | |
| } |
This file contains hidden or 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
| // | |
| // UITableView.swift | |
| // CellRegistration-Learn | |
| // | |
| // Created by Prabhu Saitu on 30/04/16. | |
| // Copyright © 2016 Prabhu Saitu. All rights reserved. | |
| // | |
| import Foundation | |
| import UIKit | |
| protocol ReusableView { | |
| static var defaultResueIdentifier:String { get } | |
| } | |
| extension ReusableView where Self: UIView { | |
| static var defaultResueIdentifier: String { | |
| return String(self) | |
| } | |
| } | |
| extension UITableViewCell: ReusableView { | |
| } | |
| protocol NibLoadableView: class { | |
| static var nibName: String { get } | |
| } | |
| extension NibLoadableView where Self:UIView { | |
| static var nibName:String { | |
| return String(self) | |
| } | |
| } | |
| extension UITableView { | |
| func register<T: UITableViewCell where T:ReusableView>(_:T.Type) { | |
| registerClass(T.self, forCellReuseIdentifier: T.defaultResueIdentifier) | |
| } | |
| func register<T: UITableViewCell where T:ReusableView, T:NibLoadableView>(_: T.Type) { | |
| let bundle = NSBundle(forClass: T.self) | |
| let nib = UINib(nibName: T.nibName, bundle: bundle) | |
| registerNib(nib, forCellReuseIdentifier: T.defaultResueIdentifier) | |
| } | |
| func dequeueReusableCell<T: UITableViewCell where T: ReusableView>(forIndexPath indexPath:NSIndexPath) -> T { | |
| guard let cell = dequeueReusableCellWithIdentifier(T.defaultResueIdentifier, forIndexPath: indexPath) as? T else { | |
| fatalError("Could not dequeue cell with identifier: \(T.defaultResueIdentifier)") | |
| } | |
| return cell | |
| } | |
| } | |
This file contains hidden or 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
| import UIKit | |
| class ViewController: UIViewController { | |
| @IBOutlet weak var tableView: UITableView! | |
| override func viewDidLoad() { | |
| super.viewDidLoad() | |
| // Do any additional setup after loading the view, typically from a nib. | |
| tableView.delegate = self | |
| tableView.dataSource = self | |
| tableView.register(BookCell.self) | |
| } | |
| override func didReceiveMemoryWarning() { | |
| super.didReceiveMemoryWarning() | |
| // Dispose of any resources that can be recreated. | |
| } | |
| } | |
| extension ViewController:UITableViewDataSource, UITableViewDelegate { | |
| func numberOfSectionsInTableView(tableView: UITableView) -> Int { | |
| return 1 | |
| } | |
| func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { | |
| return 10; | |
| } | |
| func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { | |
| let cell: BookCell = tableView.dequeueReusableCell(forIndexPath: indexPath) | |
| cell.redLabel.text = "Hello" | |
| return cell | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment