// // ViewController.swift // TestTableViewGroup // // Created by USER1 on 09.11.19. // Copyright © 2019 HSHARZ. All rights reserved. // import UIKit // WMIC memorychip get banklabel, datawidth,capacity,speed,totalwidth class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { var staedte = [Stadt]() let staedteDB:StaedteDB = StaedteDB() var group:Bool = false struct CitySection { var kontinent: Continent var staedte: [Stadt] } var sections = [CitySection]() // Referenzen @IBOutlet var labelRow: UILabel! @IBOutlet var labelInfo: UILabel! @IBOutlet var bnGroup: UIButton! @IBOutlet var bnChange: UIButton! @IBOutlet var bnNew: UIButton! @IBOutlet var tableview: UITableView! @IBAction func bnGroup(_ sender: UIButton) { group = !group tableview.reloadData() } func numberOfSections(in tableView: UITableView) -> Int { if group { return sections.count } else { return 1 } } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { if group { let section = self.sections[section] return section.staedte.count } else { return staedte.count } } // SelectecIndexChanged func tableView(_: UITableView, didSelectRowAt: IndexPath) { bnChange.isEnabled=true let row = (didSelectRowAt as NSIndexPath).row labelRow.text="row: "+String(row) if group { if let indexPath = tableview.indexPathForSelectedRow { let section = self.sections[indexPath.section] let stadt = section.staedte[indexPath.row] labelInfo.text=stadt.name } } else { if let indexPath = tableview.indexPathForSelectedRow { let stadt:Stadt = staedte[(indexPath as NSIndexPath).row] labelInfo.text=stadt.name } } } func getKontinentText(_ kontinent:Continent) -> String { switch (kontinent) { case Continent.AFRIKA: return "Afrika" case Continent.AMERIKA: return "Amerika" case Continent.ASIEN: return "Asien" case Continent.AUSTRALIA: return "Australien" case Continent.EUROPA: return "Europa" default: return "Afrika" } } func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? { if group { let section = self.sections[section] let kontinent = section.kontinent return getKontinentText(kontinent) } else { return "Liste" } } // UITableViewCellStyle.default: Bild links, Titel rechts // UITableViewCellStyle.subtitle: Bild links, Titel rechts, darunter Zusatzinformation // UITableViewCellStyle.value1: Bild links, Titel in der Mitte, rechts Zusatzinformation // UITableViewCellStyle.value2: Zusatzinformation links, rechts Titel // text, detailTextLabel, imageView func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { var cell = tableView.dequeueReusableCell(withIdentifier: "cell") if cell == nil { cell = UITableViewCell(style: UITableViewCell.CellStyle.subtitle, reuseIdentifier: "cell") } let row = (indexPath as NSIndexPath).row if group { let section = self.sections[indexPath.section] let stadt = section.staedte[indexPath.row] cell?.textLabel?.text = stadt.name cell?.detailTextLabel?.text = stadt.bemerkung switch (stadt.kontinent) { case Continent.AFRIKA: cell?.imageView!.image = UIImage(named: "schwarz") case Continent.AMERIKA: cell?.imageView!.image = UIImage(named: "rot") case Continent.ASIEN: cell?.imageView!.image = UIImage(named: "gelb") case Continent.AUSTRALIA: cell?.imageView!.image = UIImage(named: "gruen") case Continent.EUROPA: cell?.imageView!.image = UIImage(named: "blau") default: cell?.imageView!.image = UIImage(named: "schwarz") } } else { let stadt:Stadt = staedte[ row ] cell?.textLabel!.text = stadt.name cell?.detailTextLabel!.text=stadt.bemerkung switch (stadt.kontinent) { case Continent.AFRIKA: cell?.imageView!.image = UIImage(named: "schwarz") case Continent.AMERIKA: cell?.imageView!.image = UIImage(named: "rot") case Continent.ASIEN: cell?.imageView!.image = UIImage(named: "gelb") case Continent.AUSTRALIA: cell?.imageView!.image = UIImage(named: "gruen") case Continent.EUROPA: cell?.imageView!.image = UIImage(named: "blau") default: cell?.imageView!.image = UIImage(named: "schwarz") } } return cell! } // Rücksprung vom ChangeViewController Abbruch @IBAction func unwindCancel(_ segue:UIStoryboardSegue) { // keine Aktion notwendig } // unwindToEditViewCancel // Rücksprung vom ChangeViewController save @IBAction func unwindSave(_ segue:UIStoryboardSegue) { // keine Aktion notwendig } // unwindSave // Rücksprung vom ChangeViewController save oder delete @IBAction func unwindDelete(_ segue:UIStoryboardSegue) { // keine Aktion notwendig } // unwindDelete override func viewDidLoad() { super.viewDidLoad() let b=false staedte = staedteDB.loadStaedte(db: b) tableview.delegate = self tableview.dataSource = self let groups = Dictionary(grouping: self.staedte) { (stadt) in return stadt.kontinent } self.sections = groups.map { (key, values) in return CitySection(kontinent: key, staedte: values) } } }