Last active
May 9, 2019 14:48
-
-
Save e-sung/1d4e61564f875e823510e8277b75771a to your computer and use it in GitHub Desktop.
better viewcontroller with seperate datasource
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
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