Skip to content

Instantly share code, notes, and snippets.

@e-sung
Last active May 9, 2019 14:48
Show Gist options
  • Save e-sung/1d4e61564f875e823510e8277b75771a to your computer and use it in GitHub Desktop.
Save e-sung/1d4e61564f875e823510e8277b75771a to your computer and use it in GitHub Desktop.
better viewcontroller with seperate datasource
class ViewController: UIViewController {
// MARK: - View
@IBOutlet var tableView: UITableView!
@IBOutlet var segmentControl: UISegmentedControl!
// MARK: - DatSources
var dataSources:[UITableViewDataSource] = []
let catDataSource = CatDataSource()
let smileyDataSOurce = SmileyDataSource()
// MARK: - Model
let modelController = ModelController()
// MARK: - LifeCycle
override func viewDidLoad() {
super.viewDidLoad()
catDataSource.dataList = modelController.kittisList
smileyDataSOurce.dataList = modelController.smileyList
dataSources = [catDataSource, smileyDataSOurce]
tableView.dataSource = dataSources[0]
tableView.tableFooterView = UIView(frame: CGRect.zero)
}
override func viewWillAppear(_ animated: Bool) {
tableView.reloadData()
}
// MARK: - Actions
@IBAction func segementSelected(_ sender: UISegmentedControl) {
tableView.dataSource = dataSources[sender.selectedSegmentIndex]
tableView.reloadData()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment