Last active
October 1, 2018 13:03
-
-
Save DominikPetho/c115a3e8d74c3337915cda4b8fc315e1 to your computer and use it in GitHub Desktop.
SimpleViewController using enum
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
// Right now you can delete SimpleViewCellType protocol | |
struct Person { | |
var name: String | |
var surname: String | |
} | |
struct Advertisement { | |
var brandName: String | |
} | |
public final class SimpleViewController: UIViewController { | |
@IBOutlet weak var tableView: UITableView! | |
// List of instances of enum CellType | |
var peopleWithAdvertisementList: [CellType]! | |
// ................. | |
// Creates dummy list with 4 People and 2 Advertisements. | |
// We use CellType. | |
fileprivate func createDummyList() { | |
peopleWithAdvertisementList = [.person(Person(name: "Dante", surname: "Brazeal")), | |
.person(Person(name: "Sharie", surname: "Bluitt")), | |
.ad(Advertisement(brandName: "GoodRequest")), | |
.person(Person(name: "Chauncey", surname: "Stephens")), | |
.person(Person(name: "Lacey", surname: "Fanske")), | |
.ad(Advertisement(brandName: "GoodRequest"))] | |
} | |
} | |
extension SimpleViewController { | |
enum CellType { | |
case ad(Advertisement) | |
case person(Person) | |
} | |
} | |
extension SimpleViewController: UITableViewDataSource, UITableViewDelegate { | |
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { | |
var dequeuedCell: UITableViewCell? | |
// ................. | |
//Matching enumeration Values with Switch statement | |
switch peopleWithAdvertisementList[indexPath.row] { | |
case .ad(let advertisement): | |
if let cell = tableView.dequeueReusableCell(withIdentifier: "AdvertisementCell") as? AdvertisementCell { | |
cell.setup(advertisement: advertisement) | |
dequeuedCell = cell | |
} | |
case .person(let person): | |
if let cell = tableView.dequeueReusableCell(withIdentifier: "PeopleCell") as? PeopleCell { | |
cell.setup(person: person) | |
dequeuedCell = cell | |
} | |
} | |
return dequeuedCell ?? UITableViewCell() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment