Created
May 9, 2019 14:11
-
-
Save e-sung/1c22f35e41782e3c1a9c0a03e166ed18 to your computer and use it in GitHub Desktop.
Bad way of using UITableViewDataSource
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 ModelController: NSObject { | |
var kittisList = ["π±", "πΉ", "πΌ", "πΈ", "π½", "πΎ"] | |
var smileyList = ["π", "π", "π", "π", "π", "π ", "π±"] | |
} | |
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate { | |
// MARK: - View | |
@IBOutlet var tableView: UITableView! | |
@IBOutlet var segmentControl: UISegmentedControl! | |
// MARK: - Model | |
let modelController = ModelController() | |
// MARK: - LifeCycle | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
tableView.dataSource = self | |
} | |
override func viewWillAppear(_ animated: Bool) { | |
tableView.reloadData() | |
} | |
// MARK: - Actions | |
@IBAction func segementSelected(_ sender: UISegmentedControl) { | |
tableView.reloadData() | |
} | |
// MARK: - UITableViewDataSource | |
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { | |
if segmentControl.selectedSegmentIndex == 0 { | |
return modelController.kittisList.count | |
} | |
else { | |
return modelController.smileyList.count | |
} | |
} | |
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { | |
if segmentControl.selectedSegmentIndex == 0 { | |
let cell = tableView.dequeueReusableCell(withIdentifier: "catCell", for: indexPath) | |
cell.textLabel?.text = modelController.kittisList[indexPath.row] | |
return cell | |
} | |
else { | |
let cell = tableView.dequeueReusableCell(withIdentifier: "smileyCell", for: indexPath) | |
cell.textLabel?.text = modelController.smileyList[indexPath.row] | |
return cell | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment