From 2f22931e0a4617dbd218a92c02bc9e83649ee6f7 Mon Sep 17 00:00:00 2001 From: Nicholas Tay Date: Mon, 14 Mar 2022 16:05:46 +1100 Subject: Grouping items together by year https://www.ralfebert.com/ios-examples/uikit/uitableviewcontroller/grouping-sections/ --- foray/ForayTableViewController.swift | 59 ++++++++++++++++++++++++++++++------ 1 file changed, 49 insertions(+), 10 deletions(-) (limited to 'foray') diff --git a/foray/ForayTableViewController.swift b/foray/ForayTableViewController.swift index 66e9274..e283c9d 100644 --- a/foray/ForayTableViewController.swift +++ b/foray/ForayTableViewController.swift @@ -8,44 +8,80 @@ import UIKit struct MyItem { + var releaseDate: Date var id: String var name: String } +struct YearSection { + var year: Date + var items: [MyItem] +} + class ForayTableViewCell: UITableViewCell { @IBOutlet weak var cellItemName: UILabel! @IBOutlet weak var cellItemSubtitle: UILabel! @IBOutlet weak var cellItemImage: UIImageView! } +// copied from sample project +private func parseDate(_ str : String) -> Date { + let dateFormat = DateFormatter() + dateFormat.dateFormat = "yyyy-MM-dd" + return dateFormat.date(from: str)! +} + +private func firstDayOfYear(date: Date) -> Date { + let calendar = Calendar.current + let components = calendar.dateComponents([.year], from: date) + return calendar.date(from: components)! +} + 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"), + 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"), ] + var sections = [YearSection]() + + // MARK: - On load + + override func viewDidLoad() { + super.viewDidLoad() + + let groups = Dictionary(grouping: self.items) { (item) in + return firstDayOfYear(date: item.releaseDate) + } + self.sections = groups.map { (key, values) in + return YearSection(year: key, items: values) + } + // Sort the sections from oldest year to newest + self.sections.sort { (lhs, rhs) in lhs.year < rhs.year } + } + // MARK: - Table view data source override func numberOfSections(in tableView: UITableView) -> Int { // Returns number of sections for table - return 1 + return self.sections.count } override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { - // Returns number of rows for table - return items.count + // Returns number of rows for table's section + return self.sections[section].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] + let item = self.sections[indexPath.section].items[indexPath.row] cell.cellItemName?.text = item.name cell.cellItemSubtitle?.text = "Item ID: \(item.id)" cell.cellItemImage?.image = UIImage(named: item.id) @@ -54,6 +90,9 @@ class ForayTableViewController: UITableViewController { } override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? { - return "Section \(section)" + let section = self.sections[section] + let dateFormatter = DateFormatter() + dateFormatter.dateFormat = "yyyy" + return "Released in " + dateFormatter.string(from: section.year) } } -- cgit