// // MasterViewController.swift // iOSTableViewMasterDetail // // Created by Michael Wilhelm on 6/05/2016. // Copyright © 2016 Michael Wilhelm. All rights reserved. // import UIKit class MasterViewController: UITableViewController { // Hash Table Key:Value var staedte = [Stadt]() var neueStadtNr=0 var detailViewController: DetailViewController? = nil // var objects = [AnyObject]() // Hauptarray override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. staedte.append( Stadt("New York", "Manhattan") ) staedte.append( Stadt("Mailand", "Mailander Scala") ) staedte.append( Stadt("Rom", "Vatikan, Michelangelo") ) staedte.append( Stadt("Moskau", "Roter Platz") ) staedte.append( Stadt("Wernigerode", "HS Harz") ) staedte.append( Stadt("New York", "Manhattan") ) self.navigationItem.leftBarButtonItem = self.editButtonItem() let addButton = UIBarButtonItem(barButtonSystemItem: .Add, target: self, action: "insertNewObject:") self.navigationItem.rightBarButtonItem = addButton if let split = self.splitViewController { let controllers = split.viewControllers self.detailViewController = (controllers[controllers.count-1] as! UINavigationController).topViewController as? DetailViewController } } override func viewWillAppear(animated: Bool) { self.clearsSelectionOnViewWillAppear = self.splitViewController!.collapsed super.viewWillAppear(animated) } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } // func insertNewObject(sender: AnyObject) { // objects.insert(NSDate(), atIndex: 0) // let indexPath = NSIndexPath(forRow: 0, inSection: 0) // self.tableView.insertRowsAtIndexPaths([indexPath], withRowAnimation: .Automatic) // } func insertNewObject(sender: AnyObject) { neueStadtNr++ let stadt:String = "Stadt"+String(neueStadtNr) staedte.append( Stadt(stadt, stadt+stadt ) ) let indexPath = NSIndexPath(forRow: 0, inSection: 0) self.tableView.insertRowsAtIndexPaths([indexPath], withRowAnimation: .Automatic) } // MARK: - Segues override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { if segue.identifier == "showDetail" { if let indexPath = self.tableView.indexPathForSelectedRow { let stadt:Stadt = staedte[indexPath.row] // as! Stadt let detailviewcontroller = (segue.destinationViewController as! UINavigationController).topViewController as! DetailViewController detailviewcontroller.detailStadt = stadt detailviewcontroller.navigationItem.leftBarButtonItem = self.splitViewController?.displayModeButtonItem() detailviewcontroller.navigationItem.leftItemsSupplementBackButton = true } } } // override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { // if segue.identifier == "showDetail" { // if let indexPath = self.tableView.indexPathForSelectedRow { // let object = objects[indexPath.row] as! NSDate // let controller = (segue.destinationViewController as! UINavigationController).topViewController as! DetailViewController // controller.detailItem = object // controller.navigationItem.leftBarButtonItem = self.splitViewController?.displayModeButtonItem() // controller.navigationItem.leftItemsSupplementBackButton = true // } // } // } // MARK: - Table View override func numberOfSectionsInTableView(tableView: UITableView) -> Int { return 1 } override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return staedte.count } override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) let stadt:Stadt = staedte[indexPath.row] // as! NSDate cell.textLabel!.text = stadt.name return cell } override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool { // Return false if you do not want the specified item to be editable. return true } override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) { if editingStyle == .Delete { staedte.removeAtIndex(indexPath.row) tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade) } else if editingStyle == .Insert { // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view. } } }