Skip to content

Instantly share code, notes, and snippets.

@DominikPetho
Last active October 1, 2018 13:03
Show Gist options
  • Save DominikPetho/70b036e30899d7c113bdd7d4c6471ec8 to your computer and use it in GitHub Desktop.
Save DominikPetho/70b036e30899d7c113bdd7d4c6471ec8 to your computer and use it in GitHub Desktop.
Main implementation of SimpleViewController
public final class SimpleViewController: UIViewController {
@IBOutlet weak var tableView: UITableView!
// List of items, showed in TableView
var peopleWithAdvertisementList: [SimpleViewCellItem]!
public override func viewDidLoad() {
setupTableView()
createDummyList()
tableView.reloadData()
}
fileprivate func setupTableView() {
tableView.dataSource = self
tableView.delegate = self
}
// Creates dummy list with 4 People and 2 Advertisements.
// Both Types are comforming to SimpleViewCellItem protocol.
fileprivate func createDummyList() {
peopleWithAdvertisementList = [Person(name: "Dante", surname: "Brazeal"),
Person(name: "Sharie", surname: "Bluitt"),
Advertisement(brandName: "GoodRequest"),
Person(name: "Chauncey", surname: "Stephens"),
Person(name: "Lacey", surname: "Fanske"),
Advertisement(brandName: "GoodRequest")]
}
}
extension SimpleViewController: UITableViewDataSource, UITableViewDelegate {
public func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return peopleWithAdvertisementList.count
}
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var dequeuedCell: UITableViewCell?
if peopleWithAdvertisementList[indexPath.row] is Advertisement {
dequeuedCell = tableView.dequeueReusableCell(withIdentifier: "AdvertisementCell")
} else if peopleWithAdvertisementList[indexPath.row] is Person {
dequeuedCell = tableView.dequeueReusableCell(withIdentifier: "PeopleCell")
} else {
// Else what? Do we have other type comforming to SimpleViewCellItem protocol?
}
return dequeuedCell ?? UITableViewCell()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment