Advertisement
Guest User

Untitled

a guest
Oct 6th, 2015
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 13.88 KB | None | 0 0
  1.  
  2. import UIKit
  3.  
  4. class USStates : UITableViewController {
  5.    
  6.     override func viewDidLoad() {
  7.         super.viewDidLoad()
  8.     }
  9.    
  10.     override func didReceiveMemoryWarning() {
  11.         super.didReceiveMemoryWarning()
  12.     }
  13.    
  14.     // Used to hold all the state information ~ NOTE: All state pictures were gathered from their respective wikipedia page
  15.     var stateInfo = [
  16.         [
  17.             (name: "Alabama", nickname: "The Yellowhammer State", imageFileName: "alabama.png", visited: false),
  18.             (name: "Alaska", nickname: "The Last Frontier", imageFileName: "alaska.png", visited: false),
  19.             (name: "Arizona", nickname: "The Grand Canyon State", imageFileName: "arizona.png", visited: false),
  20.             (name: "Arkansas", nickname: "The Natural State", imageFileName: "arkansas.png", visited: false),
  21.         ],
  22.         [
  23.             (name: "California", nickname: "The Golden State", imageFileName: "california.png", visited: false),
  24.             (name: "Colorado", nickname: "The Centennial State", imageFileName: "colorado.png", visited: false),
  25.             (name: "Connecticut", nickname: "The Constitution State", imageFileName: "connecticut.png", visited: false),
  26.         ],
  27.         [
  28.             (name: "Delaware", nickname: "The First State", imageFileName: "delaware.png", visited: false),
  29.         ],
  30.         [
  31.             (name: "Florida", nickname: "The Sunshine State", imageFileName: "florida.png", visited: false),
  32.         ],
  33.         [
  34.             (name: "Georgia", nickname: "The Peach State", imageFileName: "georgia.png", visited: false),
  35.         ],
  36.         [
  37.             (name: "Hawaii", nickname: "The Aloha State", imageFileName: "hawaii.png", visited: false),
  38.         ],
  39.         [
  40.             (name: "Idaho", nickname: "The Gem State", imageFileName: "idaho.png", visited: false),
  41.             (name: "Illinois", nickname: "Prairie State", imageFileName: "illinois.png", visited: false),
  42.             (name: "Indiana", nickname: "The Hoosier State", imageFileName: "indiana.png", visited: false),
  43.             (name: "Iowa", nickname: "The Hawkeye State", imageFileName: "iowa.png", visited: false),
  44.         ],
  45.         [
  46.             (name: "Kansas", nickname: "The Sunflower State", imageFileName: "kansas.png", visited: false),
  47.             (name: "Kentucky", nickname: "The Bluegrass State", imageFileName: "kentucky.png", visited: false),
  48.         ],
  49.         [
  50.             (name: "Louisiana", nickname: "The Pelican State", imageFileName: "louisiana.png", visited: false),
  51.         ],
  52.         [
  53.             (name: "Maine", nickname: "The Pine Tree State", imageFileName: "maine.png", visited: false),
  54.             (name: "Maryland", nickname: "The Old Line State", imageFileName: "maryland.png", visited: false),
  55.             (name: "Massachusetts", nickname: "The Bay State", imageFileName: "massachusetts.png", visited: false),
  56.             (name: "Michigan", nickname: "The Great Lakes State", imageFileName: "michigan.png", visited: false),
  57.             (name: "Minnesota", nickname: "The North Star State", imageFileName: "minnesota.png", visited: false),
  58.             (name: "Mississippi", nickname: "The Magnolia State", imageFileName: "mississippi.png", visited: false),
  59.             (name: "Missouri", nickname: "The Show Me State", imageFileName: "missouri.png", visited: false),
  60.             (name: "Montana", nickname: "The Treasure State", imageFileName: "montana.png", visited: false),
  61.         ],
  62.         [
  63.             (name: "Nebraska", nickname: "The Cornhusker State", imageFileName: "nebraska.png", visited: false),
  64.             (name: "Nevada", nickname: "The Silver State", imageFileName: "nevada.png", visited: false),
  65.             (name: "New Hampshire", nickname: "The Granite State", imageFileName: "newhampshire.png", visited: false),
  66.             (name: "New Jersey", nickname: "The Garden State", imageFileName: "newjersey.png", visited: false),
  67.             (name: "New Mexico", nickname: "The Land of Enchantment", imageFileName: "newmexico.png", visited: false),
  68.             (name: "New York", nickname: "The Empire State", imageFileName: "newyork.png", visited: false),
  69.             (name: "North Carolina", nickname: "The Tar Heel State", imageFileName: "northcarolina.png", visited: false),
  70.             (name: "North Dakota", nickname: "The Peace Garden State", imageFileName: "northdakota.png", visited: false),
  71.         ],
  72.         [
  73.             (name: "Ohio", nickname: "The Buckeye State", imageFileName: "ohio.png", visited: false),
  74.             (name: "Oklahoma", nickname: "The Sooner State", imageFileName: "oklahoma.png", visited: false),
  75.             (name: "Oregon", nickname: "The Beaver State", imageFileName: "oregon.png", visited: false),
  76.         ],
  77.         [
  78.             (name: "Pennsylvania", nickname: "The Keystone State", imageFileName: "pennsylvania.png", visited: false),
  79.         ],
  80.         [
  81.             (name: "Rhode Island", nickname: "The Ocean State", imageFileName: "rhodeIsland.png", visited: false),
  82.         ],
  83.         [
  84.             (name: "South Carolina", nickname: "The Palmetto State", imageFileName: "southcarolina.png", visited: false),
  85.             (name: "South Dakota", nickname: "Mount Rushmore State", imageFileName: "southdakota.png", visited: false),
  86.         ],
  87.         [
  88.             (name: "Tennessee", nickname: "The Volunteer State", imageFileName: "tennessee.png", visited: false),
  89.             (name: "Texas", nickname: "The Lone Star State", imageFileName: "texas.png", visited: false),
  90.         ],
  91.         [
  92.             (name: "Utah", nickname: "The Beehive State", imageFileName: "utah.png", visited: false),
  93.         ],
  94.         [
  95.             (name: "Vermont", nickname: "The Green Mountain State", imageFileName: "vermont.png", visited: false),
  96.             (name: "Virginia", nickname: "The Old Dominion State", imageFileName: "virginia.png", visited: false),
  97.         ],
  98.         [
  99.             (name: "Washington", nickname: "The Evergreen State", imageFileName: "washington.png", visited: false),
  100.             (name: "Washington D.C.", nickname: "The Nation's Capitol", imageFileName: "washingtondc.png", visited: false),
  101.             (name: "West Virginia", nickname: "The Mountain State", imageFileName: "westvirginia.png", visited: false),
  102.             (name: "Wisconsin", nickname: "The Badger State", imageFileName: "wisconsin.png", visited: false),
  103.             (name: "Wyoming", nickname: "The Equality or Cowboy State", imageFileName: "wyoming.png", visited: false),
  104.         ]
  105.     ]
  106.    
  107.     // Used as section headers for the table
  108.     var sectionHeaders = ["A", "C", "D", "F", "G", "H", "I", "K", "L", "M", "N", "O", "P", "R", "S", "T", "U", "V", "W"]
  109.    
  110.     // Holds the value for the statesVisited label
  111.     var statesCount = 0
  112.    
  113.     // Holds the value of indexPath for when a segue occurs
  114.     var indexHolder:NSIndexPath?
  115.    
  116.     // The statesVisited label
  117.     @IBOutlet weak var statesVisited: UIBarButtonItem!
  118.    
  119.     // Counts number of sections in the table
  120.     override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
  121.         return stateInfo.count
  122.     }
  123.    
  124.     // Counts number of rows in each section
  125.     override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  126.         return stateInfo[section].count
  127.     }
  128.    
  129.     //-----------------------Reset Button Start-----------------------//
  130.    
  131.     @IBAction func resetButton(sender: UIBarButtonItem) {
  132.         // Create the alert controller
  133.         let myAlert = UIAlertController(title: "Reset All?", message: "Do you want to reset all states?", preferredStyle: .Alert)
  134.        
  135.         // Set the action for "Cancel"
  136.         myAlert.addAction(UIAlertAction(title: "Cancel", style: .Cancel, handler: nil))
  137.        
  138.         // Set the action for "Yes"
  139.         myAlert.addAction(UIAlertAction(title: "Yes", style: .Default, handler: resetStates))
  140.        
  141.         // Show the alert
  142.         presentViewController(myAlert, animated: true, completion: nil)
  143.     }
  144.    
  145.     func resetStates(alert: UIAlertAction) {
  146.        
  147.         // Set all visited values to false
  148.         for i in 0...stateInfo.count-1 {
  149.             for j in 0...stateInfo[i].count-1 {
  150.                 stateInfo[i][j].visited = false;
  151.             }
  152.         }
  153.        
  154.         // Reload the entire table
  155.         self.tableView.reloadData()
  156.        
  157.         // Set statesCount to 0
  158.         statesCount = 0
  159.        
  160.         // Updates the statesVisited label
  161.         statesVisited.title = "Visited \(statesCount)"
  162.        
  163.     }
  164.    
  165.     //-----------------------Reset Button End-----------------------//
  166.    
  167.     //-----------------------Create Table Start-----------------------//
  168.    
  169.     // Create the cells
  170.     override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
  171.         let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)
  172.        
  173.         // Sets the text label to the name of the state
  174.         cell.textLabel?.text = stateInfo[indexPath.section][indexPath.row].name
  175.        
  176.         // Sets the detail text label to the nickname of the state
  177.         cell.detailTextLabel?.text = stateInfo[indexPath.section][indexPath.row].nickname
  178.        
  179.         return cell
  180.     }
  181.    
  182.     // Create the section headers
  183.     override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
  184.         return sectionHeaders[section]
  185.     }
  186.    
  187.     // Change color of cell depending on visited status
  188.     override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell,
  189.         forRowAtIndexPath indexPath: NSIndexPath) {
  190.            
  191.             // Change background to a pastel green if visited
  192.             if stateInfo[indexPath.section][indexPath.row].visited{
  193.                 cell.backgroundColor = UIColor(red: 189/255, green: 236/255, blue: 182/255, alpha: 1)
  194.             }
  195.             // Otherwise change the background color to white
  196.             else {
  197.                 cell.backgroundColor = UIColor.whiteColor()
  198.             }
  199.     }
  200.    
  201.     //-----------------------Create Table End-----------------------//
  202.  
  203.     //-----------------------User Cell Interation Start-----------------------//
  204.    
  205.     override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
  206.        
  207.         // If cell selected has a visited value of false...
  208.         if stateInfo[indexPath.section][indexPath.row].visited == false {
  209.            
  210.             // ...set value to true and update statesCount
  211.             stateInfo[indexPath.section][indexPath.row].visited = true
  212.             statesCount++
  213.            
  214.         }
  215.         // Otherwise if selected has a visited value of true...
  216.         else if stateInfo[indexPath.section][indexPath.row].visited == true {
  217.            
  218.             // ...create an alert and ask the user if they want to reset the cell
  219.            
  220.             // Create the alert controller
  221.             let myAlert = UIAlertController(title: "Reset State?", message: "Do you want to reset \(stateInfo[indexPath.section][indexPath.row].name)?", preferredStyle: .Alert)
  222.            
  223.             // Set the action for "Cancel"
  224.             myAlert.addAction(UIAlertAction(title: "Cancel", style: .Cancel, handler: nil))
  225.            
  226.             // Set the action for "Yes"
  227.             myAlert.addAction(UIAlertAction(title: "Yes", style: .Default) {
  228.                
  229.                 // Set visited to true and update statecount
  230.                 alert in self.stateInfo[indexPath.section][indexPath.row].visited = false
  231.                 self.statesCount--
  232.                
  233.                 // Reload the updated cell to change color and update the visited label
  234.                 tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.None)
  235.                 self.statesVisited.title = "Visited \(self.statesCount)"
  236.                 })
  237.            
  238.             // Show the alert
  239.             presentViewController(myAlert, animated: true, completion: nil)
  240.         }
  241.        
  242.         // Reload the updated cell to change color
  243.         tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.None)
  244.        
  245.         // Updated the statesVisited label
  246.         self.statesVisited.title = "Visited: \(self.statesCount)"
  247.     }
  248.    
  249.     // Detects details button tapped and calls a segue
  250.     override func tableView(tableView: UITableView, accessoryButtonTappedForRowWithIndexPath indexPath: NSIndexPath) {
  251.        
  252.         // indexHolder stores the indexPath of the cell that the tapped accessory button is in
  253.         self.indexHolder = indexPath
  254.        
  255.         // Performs the segue "statesSegue"
  256.         self.performSegueWithIdentifier("statesSegue", sender: indexPath)
  257.     }
  258.    
  259.     //-----------------------User Cell Interation End-----------------------//
  260.  
  261.     // Prepares for a segue
  262.     override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
  263.        
  264.         // If the segue is "statesSegue"...
  265.         if segue.identifier == "statesSegue" {
  266.            
  267.             // ...let destinationVC be equal to the destination ViewController...
  268.             if let destinationVC = segue.destinationViewController as? ViewController {
  269.                
  270.                 // ...and get the column/row values of the cell from indexHolder...
  271.                 if let column = indexHolder?.section {
  272.                    
  273.                      let row = indexHolder?.row
  274.                    
  275.                         // ...and set the stateName to the specified state...
  276.                         destinationVC.stateName = stateInfo[column][row!].name
  277.                    
  278.                         // ...as well as the imageFile to the specified imageFileName
  279.                         destinationVC.imageFile = stateInfo[column][row!].imageFileName
  280.                 }
  281.             }
  282.         }
  283.     }
  284. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement