Let’s continue on from Part 1, and add a new table view to our app. In our storyboard, let’s add a table view controller by searching in the bottom right and dragging.
Next, let’s add a button to our main view controller that will link to our new table view controller. Similar to what we did with the web view, Ctrl + click on this button and drag it to the newly created table view controller.Upon release, choose push.
Now, let’s make sure everything works properly. Hit the large play button and click on Table View. You should now be taken to a blank table:
Let’s populate this table with some text. Go to File -> New -> File and choose a Cocoa Touch Class. Let’s call this file TableViewController, and make this a subclass of UITableViewController in the Swift language.
Once the file is saved, we’ll be presented with a file with some boilerplate code. On the first line in our class file, let’s declare a constant. This constant will be an array of strings that will be inserted into our table:
let tableArray: NSArray = ["Apple", "Orange", "Banana", "Grape", "Kiwi"]
Let’s modify the function that has this signature:
func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int
This function returns the number of rows in our table view. Instead of setting this to zero, let’s change this to ten. Next, let’s uncomment the function that has this signature:
override func numberOfSectionsInTableView(tableView: UITableView!) -> Int
This function controls how many sections we will have in our table view. Let’s modify this function to return 1.
Finally, let’s add a function that will populate our cells:
override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "MyTestCell")
cell.textLabel.text = tableArray.objectAtIndex(indexPath.row) as NSString
return cell
}
This function iterates through each row in our table and sets the text value to be equal to the fruits we declared at the top of the class file. The final file should look like this:
class TableViewController: UITableViewController {
let tableArray: NSArray = ["Apple", "Orange", "Banana", "Grape", "Kiwi"]
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView!) -> Int {
// #warning Potentially incomplete method implementation.
// Return the number of sections.
return 1
}
override func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete method implementation.
// Return the number of rows in the section.
return tableArray.count
}
override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "MyTestCell")
cell.textLabel.text = tableArray.objectAtIndex(indexPath.row) as NSString
return cell
}
}
Finally, we need to go back to our storyboard and link to our custom table view controller class. Select the storyboard, click on the table view controller, choose the identity inspector and fill in TableViewController for the custom class.
If we click the play button to build our project and then click on our table view button, we should see our table populated with names of fruit:
Click on the Sample Swift App icon in the top left of the screen and then choose Build Phases. Under Link Binary with Libraries, click the plus button and search for MapKit. Once found, click Add:
In the story board, add another view controller. Search for a MKMapView and drag it into the newly created controller. In the main navigation controller, create another button named Map View, Ctrl + click + drag to the newly created view controller, and upon release choose push:
Additionally, choose the Map View in the storyboard, click on the connections inspector, Ctrl + click on delegate and drag to the map view controller.
Next, let’s create a custom view controller that will control our map view. Go to File -> New -> File and choose Cocoa Touch. Let’s call this file MapViewController and inherit from UIViewController.
Let’s now link our map view in our storyboard to our newly created map view controller file. In the storyboard, Ctrl + click on the map view and drag to our Map View Controller to create an IBOutlet variable.
It should look something like this:
@IBOutlet var mapView: MKMapView!
Let’s add some code to our controller that will display the map around Apple’s campus in Cupertino, CA. I’ve looked up the GPS coordinates already, so here is what the completed code should look like:
import UIKit
import MapKit
class MapViewController: UIViewController, MKMapViewDelegate {
@IBOutlet var mapView: MKMapView!
override func viewDidLoad() {
super.viewDidLoad()
let latitude:CLLocationDegrees = 37.331789
let longitude:CLLocationDegrees = -122.029620
let latitudeDelta:CLLocationDegrees = 0.01
let longitudeDelta:CLLocationDegrees = 0.01
let span:MKCoordinateSpan = MKCoordinateSpan(latitudeDelta: latitudeDelta, longitudeDelta: longitudeDelta)
let location:CLLocationCoordinate2D = CLLocationCoordinate2DMake(latitude, longitude)
let region: MKCoordinateRegion = MKCoordinateRegionMake(location, span)
self.mapView.setRegion(region, animated: true)
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
This should now build, and when you click on the Map View button, you should be able to see a map showing Apple’s campus at the center of the screen.
Ryan is a software engineer and electronic dance music producer currently residing in San Francisco, CA. Ryan started up as a biomedical engineer but fell in love with web/mobile programming after building his first Android app. You can find him on GitHub @rloomba.