From 6575fcb169596a659b1a185ab53a6d27520de1d6 Mon Sep 17 00:00:00 2001 From: Nicholas Tay Date: Mon, 14 Mar 2022 16:32:59 +1100 Subject: Second type of custom cell Not sure if this is the correct way of switching into a different cell type with the prototypes, but it works. To review. --- foray/Base.lproj/Main.storyboard | 62 +++++++++++++++++++++++++++++++++++- foray/ForayTableViewController.swift | 33 +++++++++++++------ 2 files changed, 85 insertions(+), 10 deletions(-) (limited to 'foray') diff --git a/foray/Base.lproj/Main.storyboard b/foray/Base.lproj/Main.storyboard index 7af2493..c5181ad 100644 --- a/foray/Base.lproj/Main.storyboard +++ b/foray/Base.lproj/Main.storyboard @@ -2,7 +2,8 @@ - + + @@ -57,6 +58,59 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -70,4 +124,10 @@ + + + + + + diff --git a/foray/ForayTableViewController.swift b/foray/ForayTableViewController.swift index e283c9d..dc32611 100644 --- a/foray/ForayTableViewController.swift +++ b/foray/ForayTableViewController.swift @@ -7,7 +7,13 @@ import UIKit +enum ItemType { + case item + case quest +} + struct MyItem { + var type: ItemType var releaseDate: Date var id: String var name: String @@ -42,11 +48,14 @@ class ForayTableViewController: UITableViewController { // MARK: - Static data TEMP let items = [ - MyItem(releaseDate: parseDate("2006-05-26"), id: "mh", name: "Miners Helmet"), - MyItem(releaseDate: parseDate("2010-05-01"), id: "it", name: "Inner Tube"), - MyItem(releaseDate: parseDate("2009-04-24"), id: "tbg", name: "Toboggan"), - MyItem(releaseDate: parseDate("2006-03-29"), id: "spy", name: "Spy Phone"), - MyItem(releaseDate: parseDate("2008-11-18"), id: "bnb", name: "Black Ninja Belt"), + MyItem(type: .item, releaseDate: parseDate("2006-05-26"), id: "mh", name: "Miners Helmet"), + MyItem(type: .item, releaseDate: parseDate("2010-05-01"), id: "it", name: "Inner Tube"), + MyItem(type: .item, releaseDate: parseDate("2009-04-24"), id: "tbg", name: "Toboggan"), + MyItem(type: .item, releaseDate: parseDate("2006-03-29"), id: "spy", name: "Spy Phone"), + MyItem(type: .item, releaseDate: parseDate("2008-11-18"), id: "bnb", name: "Black Ninja Belt"), + MyItem(type: .quest, releaseDate: parseDate("2006-05-23"), id: "cmp", name: "Case of the Missing Puffles"), + MyItem(type: .quest, releaseDate: parseDate("2009-11-16"), id: "gsm", name: "G's Secret Mission"), + MyItem(type: .quest, releaseDate: parseDate("2009-04-18"), id: "cmc", name: "Case of the Missing Coins"), ] var sections = [YearSection]() @@ -79,12 +88,18 @@ class ForayTableViewController: UITableViewController { } override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { - let cell = tableView.dequeueReusableCell(withIdentifier: "ForayCell", for: indexPath) as! ForayTableViewCell - let item = self.sections[indexPath.section].items[indexPath.row] + let cell: ForayTableViewCell + switch item.type { + case .item: + cell = tableView.dequeueReusableCell(withIdentifier: "ForayCell", for: indexPath) as! ForayTableViewCell + cell.cellItemImage?.image = UIImage(named: item.id) + case .quest: + cell = tableView.dequeueReusableCell(withIdentifier: "ForayQuestCell", for: indexPath) as! ForayTableViewCell + } + cell.cellItemName?.text = item.name - cell.cellItemSubtitle?.text = "Item ID: \(item.id)" - cell.cellItemImage?.image = UIImage(named: item.id) + cell.cellItemSubtitle?.text = "ID: " + item.id return cell } -- cgit