From a8c3db459cc1be105dd910efb8bbc08bc163fb91 Mon Sep 17 00:00:00 2001 From: Nicholas Tay Date: Mon, 14 Mar 2022 15:47:36 +1100 Subject: Learning UITableViewController: array data + custom cells Forgot to commit after finishing just the using array data bit. So committing with the custom cells done. https://www.ralfebert.com/ios-examples/uikit/uitableviewcontroller/ --- foray/ForayTableViewController.swift | 59 ++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 foray/ForayTableViewController.swift (limited to 'foray/ForayTableViewController.swift') diff --git a/foray/ForayTableViewController.swift b/foray/ForayTableViewController.swift new file mode 100644 index 0000000..66e9274 --- /dev/null +++ b/foray/ForayTableViewController.swift @@ -0,0 +1,59 @@ +// +// ForayTableViewController.swift +// foray +// +// Created by Nicholas Tay on 14/3/2022. +// + +import UIKit + +struct MyItem { + var id: String + var name: String +} + +class ForayTableViewCell: UITableViewCell { + @IBOutlet weak var cellItemName: UILabel! + @IBOutlet weak var cellItemSubtitle: UILabel! + @IBOutlet weak var cellItemImage: UIImageView! +} + +class ForayTableViewController: UITableViewController { + + // MARK: - Static data TEMP + + let items = [ + MyItem(id: "mh", name: "Miners Helmet"), + MyItem(id: "it", name: "Inner Tube"), + MyItem(id: "tbg", name: "Toboggan"), + MyItem(id: "spy", name: "Spy Phone"), + MyItem(id: "bnb", name: "Black Ninja Belt"), + ] + + // MARK: - Table view data source + + override func numberOfSections(in tableView: UITableView) -> Int { + // Returns number of sections for table + return 1 + } + + override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { + // Returns number of rows for table + return items.count + } + + override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { + let cell = tableView.dequeueReusableCell(withIdentifier: "ForayCell", for: indexPath) as! ForayTableViewCell + + let item = items[indexPath.row] + cell.cellItemName?.text = item.name + cell.cellItemSubtitle?.text = "Item ID: \(item.id)" + cell.cellItemImage?.image = UIImage(named: item.id) + + return cell + } + + override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? { + return "Section \(section)" + } +} -- cgit