Last active
July 22, 2017 08:18
-
-
Save abhishekbedi1432/98967d3174fda802345b452cac5445f5 to your computer and use it in GitHub Desktop.
Using different datasources with the same UITableView
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
//: Playground - noun: a place where people can play | |
import UIKit | |
import XCPlayground | |
//: Protocols | |
protocol TableViewDisplayable { | |
var title:String {get set} | |
} | |
protocol DataProvider { | |
associatedtype Data : TableViewDisplayable | |
var data: [Data] { get set } | |
static func dummyData() -> [Data] | |
} | |
extension DataProvider { | |
func numberOfRows() -> Int { | |
return self.data.count; | |
} | |
} | |
//: Models | |
class SportsPerson: TableViewDisplayable { | |
var title:String | |
init(name:String) { | |
self.title = name | |
} | |
} | |
class MovieStar: TableViewDisplayable { | |
var title:String | |
init(name:String) { | |
self.title = name | |
} | |
} | |
//: Datasources | |
class SportsDatasource : NSObject, DataProvider, UITableViewDataSource { | |
typealias Data = SportsPerson | |
var data: [Data] | |
override init() { | |
self.data = SportsDatasource.dummyData() | |
} | |
static func dummyData() -> [Data] { | |
return [ | |
SportsPerson(name: "Christiani Ronaldo"), | |
SportsPerson(name: "Michael Jordon"), | |
SportsPerson(name: "Clayton Kershaw"), | |
SportsPerson(name: "Tiger Woods"), | |
SportsPerson(name: "Roger Federrer"), | |
SportsPerson(name: "Sachin Tendulkar"), | |
SportsPerson(name: "Lewis Hamilton"), | |
] | |
} | |
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { | |
return self.numberOfRows() | |
} | |
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { | |
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as UITableViewCell | |
cell.accessoryType = .detailButton | |
let data = self.data[indexPath.row] | |
cell.textLabel?.text = data.title | |
return cell | |
} | |
} | |
class MovieStarsDatasource :NSObject, DataProvider, UITableViewDataSource { | |
typealias Data = MovieStar | |
var data: [Data] | |
override init() { | |
self.data = MovieStarsDatasource.dummyData() | |
} | |
static func dummyData() -> [Data] { | |
return [ | |
MovieStar(name: "Leonardo DiCaprio"), | |
MovieStar(name: "Will Smith"), | |
MovieStar(name: "Johny Depp"), | |
MovieStar(name: "Christian Bale"), | |
MovieStar(name: "Ben Affleck"), | |
MovieStar(name: "Robert Downey, Jr"), | |
MovieStar(name: "Leonardo DiCaprio") | |
] | |
} | |
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { | |
return self.numberOfRows() | |
} | |
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { | |
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as UITableViewCell | |
cell.accessoryType = .disclosureIndicator | |
let data = self.data[indexPath.row] | |
cell.textLabel?.text = data.title | |
return cell | |
} | |
} | |
class ViewController: UIViewController { | |
var tableView: UITableView! | |
// Switch between datasources here ... | |
var datasource = MovieStarsDatasource() | |
// var datasource = MySportPersonDataSource() | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
self.view.frame = CGRect(x: 0, y: 0, width: 320, height: 480) | |
self.tableView = UITableView(frame:self.view.frame) | |
self.tableView!.dataSource = datasource | |
self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell") | |
self.view.addSubview(self.tableView) | |
} | |
} | |
var ctrl = ViewController() | |
XCPShowView(identifier: "Playground VC", view: ctrl.view) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment