Skip to content

Instantly share code, notes, and snippets.

@e-sung
Created May 9, 2019 14:11
Show Gist options
  • Save e-sung/1c22f35e41782e3c1a9c0a03e166ed18 to your computer and use it in GitHub Desktop.
Save e-sung/1c22f35e41782e3c1a9c0a03e166ed18 to your computer and use it in GitHub Desktop.
Bad way of using UITableViewDataSource
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