Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import UIKit
- class USStates : UITableViewController {
- override func viewDidLoad() {
- super.viewDidLoad()
- }
- override func didReceiveMemoryWarning() {
- super.didReceiveMemoryWarning()
- }
- // Used to hold all the state information ~ NOTE: All state pictures were gathered from their respective wikipedia page
- var stateInfo = [
- [
- (name: "Alabama", nickname: "The Yellowhammer State", imageFileName: "alabama.png", visited: false),
- (name: "Alaska", nickname: "The Last Frontier", imageFileName: "alaska.png", visited: false),
- (name: "Arizona", nickname: "The Grand Canyon State", imageFileName: "arizona.png", visited: false),
- (name: "Arkansas", nickname: "The Natural State", imageFileName: "arkansas.png", visited: false),
- ],
- [
- (name: "California", nickname: "The Golden State", imageFileName: "california.png", visited: false),
- (name: "Colorado", nickname: "The Centennial State", imageFileName: "colorado.png", visited: false),
- (name: "Connecticut", nickname: "The Constitution State", imageFileName: "connecticut.png", visited: false),
- ],
- [
- (name: "Delaware", nickname: "The First State", imageFileName: "delaware.png", visited: false),
- ],
- [
- (name: "Florida", nickname: "The Sunshine State", imageFileName: "florida.png", visited: false),
- ],
- [
- (name: "Georgia", nickname: "The Peach State", imageFileName: "georgia.png", visited: false),
- ],
- [
- (name: "Hawaii", nickname: "The Aloha State", imageFileName: "hawaii.png", visited: false),
- ],
- [
- (name: "Idaho", nickname: "The Gem State", imageFileName: "idaho.png", visited: false),
- (name: "Illinois", nickname: "Prairie State", imageFileName: "illinois.png", visited: false),
- (name: "Indiana", nickname: "The Hoosier State", imageFileName: "indiana.png", visited: false),
- (name: "Iowa", nickname: "The Hawkeye State", imageFileName: "iowa.png", visited: false),
- ],
- [
- (name: "Kansas", nickname: "The Sunflower State", imageFileName: "kansas.png", visited: false),
- (name: "Kentucky", nickname: "The Bluegrass State", imageFileName: "kentucky.png", visited: false),
- ],
- [
- (name: "Louisiana", nickname: "The Pelican State", imageFileName: "louisiana.png", visited: false),
- ],
- [
- (name: "Maine", nickname: "The Pine Tree State", imageFileName: "maine.png", visited: false),
- (name: "Maryland", nickname: "The Old Line State", imageFileName: "maryland.png", visited: false),
- (name: "Massachusetts", nickname: "The Bay State", imageFileName: "massachusetts.png", visited: false),
- (name: "Michigan", nickname: "The Great Lakes State", imageFileName: "michigan.png", visited: false),
- (name: "Minnesota", nickname: "The North Star State", imageFileName: "minnesota.png", visited: false),
- (name: "Mississippi", nickname: "The Magnolia State", imageFileName: "mississippi.png", visited: false),
- (name: "Missouri", nickname: "The Show Me State", imageFileName: "missouri.png", visited: false),
- (name: "Montana", nickname: "The Treasure State", imageFileName: "montana.png", visited: false),
- ],
- [
- (name: "Nebraska", nickname: "The Cornhusker State", imageFileName: "nebraska.png", visited: false),
- (name: "Nevada", nickname: "The Silver State", imageFileName: "nevada.png", visited: false),
- (name: "New Hampshire", nickname: "The Granite State", imageFileName: "newhampshire.png", visited: false),
- (name: "New Jersey", nickname: "The Garden State", imageFileName: "newjersey.png", visited: false),
- (name: "New Mexico", nickname: "The Land of Enchantment", imageFileName: "newmexico.png", visited: false),
- (name: "New York", nickname: "The Empire State", imageFileName: "newyork.png", visited: false),
- (name: "North Carolina", nickname: "The Tar Heel State", imageFileName: "northcarolina.png", visited: false),
- (name: "North Dakota", nickname: "The Peace Garden State", imageFileName: "northdakota.png", visited: false),
- ],
- [
- (name: "Ohio", nickname: "The Buckeye State", imageFileName: "ohio.png", visited: false),
- (name: "Oklahoma", nickname: "The Sooner State", imageFileName: "oklahoma.png", visited: false),
- (name: "Oregon", nickname: "The Beaver State", imageFileName: "oregon.png", visited: false),
- ],
- [
- (name: "Pennsylvania", nickname: "The Keystone State", imageFileName: "pennsylvania.png", visited: false),
- ],
- [
- (name: "Rhode Island", nickname: "The Ocean State", imageFileName: "rhodeIsland.png", visited: false),
- ],
- [
- (name: "South Carolina", nickname: "The Palmetto State", imageFileName: "southcarolina.png", visited: false),
- (name: "South Dakota", nickname: "Mount Rushmore State", imageFileName: "southdakota.png", visited: false),
- ],
- [
- (name: "Tennessee", nickname: "The Volunteer State", imageFileName: "tennessee.png", visited: false),
- (name: "Texas", nickname: "The Lone Star State", imageFileName: "texas.png", visited: false),
- ],
- [
- (name: "Utah", nickname: "The Beehive State", imageFileName: "utah.png", visited: false),
- ],
- [
- (name: "Vermont", nickname: "The Green Mountain State", imageFileName: "vermont.png", visited: false),
- (name: "Virginia", nickname: "The Old Dominion State", imageFileName: "virginia.png", visited: false),
- ],
- [
- (name: "Washington", nickname: "The Evergreen State", imageFileName: "washington.png", visited: false),
- (name: "Washington D.C.", nickname: "The Nation's Capitol", imageFileName: "washingtondc.png", visited: false),
- (name: "West Virginia", nickname: "The Mountain State", imageFileName: "westvirginia.png", visited: false),
- (name: "Wisconsin", nickname: "The Badger State", imageFileName: "wisconsin.png", visited: false),
- (name: "Wyoming", nickname: "The Equality or Cowboy State", imageFileName: "wyoming.png", visited: false),
- ]
- ]
- // Used as section headers for the table
- var sectionHeaders = ["A", "C", "D", "F", "G", "H", "I", "K", "L", "M", "N", "O", "P", "R", "S", "T", "U", "V", "W"]
- // Holds the value for the statesVisited label
- var statesCount = 0
- // Holds the value of indexPath for when a segue occurs
- var indexHolder:NSIndexPath?
- // The statesVisited label
- @IBOutlet weak var statesVisited: UIBarButtonItem!
- // Counts number of sections in the table
- override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
- return stateInfo.count
- }
- // Counts number of rows in each section
- override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
- return stateInfo[section].count
- }
- //-----------------------Reset Button Start-----------------------//
- @IBAction func resetButton(sender: UIBarButtonItem) {
- // Create the alert controller
- let myAlert = UIAlertController(title: "Reset All?", message: "Do you want to reset all states?", preferredStyle: .Alert)
- // Set the action for "Cancel"
- myAlert.addAction(UIAlertAction(title: "Cancel", style: .Cancel, handler: nil))
- // Set the action for "Yes"
- myAlert.addAction(UIAlertAction(title: "Yes", style: .Default, handler: resetStates))
- // Show the alert
- presentViewController(myAlert, animated: true, completion: nil)
- }
- func resetStates(alert: UIAlertAction) {
- // Set all visited values to false
- for i in 0...stateInfo.count-1 {
- for j in 0...stateInfo[i].count-1 {
- stateInfo[i][j].visited = false;
- }
- }
- // Reload the entire table
- self.tableView.reloadData()
- // Set statesCount to 0
- statesCount = 0
- // Updates the statesVisited label
- statesVisited.title = "Visited \(statesCount)"
- }
- //-----------------------Reset Button End-----------------------//
- //-----------------------Create Table Start-----------------------//
- // Create the cells
- override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
- let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)
- // Sets the text label to the name of the state
- cell.textLabel?.text = stateInfo[indexPath.section][indexPath.row].name
- // Sets the detail text label to the nickname of the state
- cell.detailTextLabel?.text = stateInfo[indexPath.section][indexPath.row].nickname
- return cell
- }
- // Create the section headers
- override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
- return sectionHeaders[section]
- }
- // Change color of cell depending on visited status
- override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell,
- forRowAtIndexPath indexPath: NSIndexPath) {
- // Change background to a pastel green if visited
- if stateInfo[indexPath.section][indexPath.row].visited{
- cell.backgroundColor = UIColor(red: 189/255, green: 236/255, blue: 182/255, alpha: 1)
- }
- // Otherwise change the background color to white
- else {
- cell.backgroundColor = UIColor.whiteColor()
- }
- }
- //-----------------------Create Table End-----------------------//
- //-----------------------User Cell Interation Start-----------------------//
- override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
- // If cell selected has a visited value of false...
- if stateInfo[indexPath.section][indexPath.row].visited == false {
- // ...set value to true and update statesCount
- stateInfo[indexPath.section][indexPath.row].visited = true
- statesCount++
- }
- // Otherwise if selected has a visited value of true...
- else if stateInfo[indexPath.section][indexPath.row].visited == true {
- // ...create an alert and ask the user if they want to reset the cell
- // Create the alert controller
- let myAlert = UIAlertController(title: "Reset State?", message: "Do you want to reset \(stateInfo[indexPath.section][indexPath.row].name)?", preferredStyle: .Alert)
- // Set the action for "Cancel"
- myAlert.addAction(UIAlertAction(title: "Cancel", style: .Cancel, handler: nil))
- // Set the action for "Yes"
- myAlert.addAction(UIAlertAction(title: "Yes", style: .Default) {
- // Set visited to true and update statecount
- alert in self.stateInfo[indexPath.section][indexPath.row].visited = false
- self.statesCount--
- // Reload the updated cell to change color and update the visited label
- tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.None)
- self.statesVisited.title = "Visited \(self.statesCount)"
- })
- // Show the alert
- presentViewController(myAlert, animated: true, completion: nil)
- }
- // Reload the updated cell to change color
- tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.None)
- // Updated the statesVisited label
- self.statesVisited.title = "Visited: \(self.statesCount)"
- }
- // Detects details button tapped and calls a segue
- override func tableView(tableView: UITableView, accessoryButtonTappedForRowWithIndexPath indexPath: NSIndexPath) {
- // indexHolder stores the indexPath of the cell that the tapped accessory button is in
- self.indexHolder = indexPath
- // Performs the segue "statesSegue"
- self.performSegueWithIdentifier("statesSegue", sender: indexPath)
- }
- //-----------------------User Cell Interation End-----------------------//
- // Prepares for a segue
- override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
- // If the segue is "statesSegue"...
- if segue.identifier == "statesSegue" {
- // ...let destinationVC be equal to the destination ViewController...
- if let destinationVC = segue.destinationViewController as? ViewController {
- // ...and get the column/row values of the cell from indexHolder...
- if let column = indexHolder?.section {
- let row = indexHolder?.row
- // ...and set the stateName to the specified state...
- destinationVC.stateName = stateInfo[column][row!].name
- // ...as well as the imageFile to the specified imageFileName
- destinationVC.imageFile = stateInfo[column][row!].imageFileName
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement