// // MasterViewController.swift // iOSMasterDetailV6 // // Created by on 07.06.16. // Copyright (c) 2016 HSHarz. All rights reserved. // import UIKit class MasterViewController: UITableViewController { var staedte = [Stadt]() var neueStadtNr=0 override func awakeFromNib() { super.awakeFromNib() } override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. self.navigationItem.leftBarButtonItem = self.editButtonItem() let addButton = UIBarButtonItem(barButtonSystemItem: .Add, target: self, action: "insertNewObject:") self.navigationItem.rightBarButtonItem = addButton 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") ) } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } func insertNewObject(sender: AnyObject) { //objects.insert(NSDate(), atIndex: 0) 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 = staedte[indexPath.row] // (segue.destinationViewController as DetailViewController).detailItem = stadt // } // } // } override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { if segue.identifier == "showDetail" { if let indexPath = self.tableView.indexPathForSelectedRow() { let stadt = staedte[indexPath.row] let detailviewcontroller = (segue.destinationViewController as DetailViewController) detailviewcontroller.changed=true detailviewcontroller.detailItem = stadt } } } // 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 // } // } // } // 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) as UITableViewCell //let object = objects[indexPath.row] as NSDate //cell.textLabel!.text = object.description 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. } } }