diff options
author | Nicholas Tay <nick@windblume.net> | 2022-03-14 15:47:36 +1100 |
---|---|---|
committer | Nicholas Tay <nick@windblume.net> | 2022-03-14 16:06:32 +1100 |
commit | a8c3db459cc1be105dd910efb8bbc08bc163fb91 (patch) | |
tree | 2a523f0a71adc1be28fff0c9881e43f579ce3ab1 /foray/ForayTableViewController.swift | |
parent | 2dc7c2df9ba4f6201d3dcefaa77a52425a65e2c7 (diff) | |
download | forayios-a8c3db459cc1be105dd910efb8bbc08bc163fb91.tar.gz forayios-a8c3db459cc1be105dd910efb8bbc08bc163fb91.tar.bz2 forayios-a8c3db459cc1be105dd910efb8bbc08bc163fb91.zip |
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/
Diffstat (limited to '')
-rw-r--r-- | foray/ForayTableViewController.swift | 59 |
1 files changed, 59 insertions, 0 deletions
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)" + } +} |