Upgrade to Pro — share decks privately, control downloads, hide ads and more …

MapStack - lesson 4

leveton
March 11, 2016
30

MapStack - lesson 4

leveton

March 11, 2016
Tweet

Transcript

  1. Parsing JSON Before we get into networking, we use a

    dummy JSON file for our datasource. NSData, NSBundle, and NSJSONSerialization are all subclasses of NSObject. Get familiar with the class methods shown here. They’re used often for converting data from the filesystem or some other source into Objective-C code.
  2. Parsing JSON Now that we have an Objective-C friendly model

    of our data in the form of an NSDictionary, we grab an array from the dictionary, and enumerate through the array using fast-enumeration. Finally we use a custom selector to convert the ‘object’ in Javascript Object Notation into an MSLocation.
  3. Annotating our map Notice at the end we can add

    a location object as an annotation on our map? In the MSLocation header file we’ve added <MKAnnotation> Next to our interface declaration.
  4. Annotating our map This means that MSLocation conforms to the

    MKAnnotaion protocol. So as long as MSLocation follows certain rules (in this case setting a coordinate struct and a title string) The map will drop pins based on the coordinates that we set, and, if you tap on the pin, display the title string that we set.
  5. Understanding NSIndexPath NSIndexPath has a row and a section property.

    Think of sections as columns. Our locations table has only one column but our settings table will have three.
  6. Understanding NSIndexPath Switch to the lesson 8 branch and run

    the app to see this more clearly. Our locations table only has one section so indexPath.section will be 0, we care about is indexPath.row
  7. Use setLocation to build a detail view Now our goal

    is to populate the table in MSLocationsViewController. We also need to allow our users to tap on any cell and get a detail view of that location. At the end of populateMap, we get a reference to our MSLocationsViewController by grabbing it from the tab bar that handles the navigation for our app. We set its datasource with the new data, reloading the table in the setter’s implementation.
  8. Use setLocation to build a detail view In MSLocationsViewController, we

    give MSLocationDetailViewController a single location object so that it can build a full location for the user. Typically setting up the detail is begun in our didSelectRowAtIndexPath delegate method:
  9. Understanding dequeue table view cell Backing up a little we

    need to review cellForRowAtIndexPath. One of the most used and important methods in iOS. Let’s change our numberOfRowsInSection to 1000 and implement our cellForRow delegate method like this:
  10. Understanding dequeue table view cell If you run the app

    now and go to locations, you’ll see ‘new cell’ logged out 9 times on an iPhone 4 and 15 times on an iPhone 6-plus. And then ‘old cell’ logged out as soon as you scroll up. ‘old cell’ is logged until you reach the 1000th cell.
  11. Understanding dequeue table view cell What’s happening is that the

    number of immediately visible cells are created as new cells. When the cells are no longer visible (after scrolling) they are reused by the table. This spares the system from having to create a thousand new objects. Why 9 on the smaller iPhone and 15 on the larger phone? Because the larger phone has more immediately visible cells!
  12. Understanding reuse identifier Looking again at our implementation. We pass

    the dequeueReusableCellWithIdentifier instance method an identifier. The identifier tells the table view that we want the same type of cell to always be reused. A ‘location’ cell in this case. If you had more than one TYPE of cell on your table, something besides a location cell, you would have to create another UITableViewCell object and give it a different identifier.
  13. Understanding reuse identifier The vast majority of the time, you’ll

    only use one type of cell and therefore one identifier for your table.
  14. presenting a modal Back to didSelectRowAtIndexPath, We give MSLocationDetailViewController a

    single location object so that it can build a full location for the user. Upon tap, we call presentViewController:animated: on our current view controller which will cause the detail view to present itself from the bottom of the device. (Make sure ‘animated’ is set to YES).
  15. Presenting a modal is one of the two main ways

    of presenting new views onto existing views in iOS. We’ll go over the other (popping a view onto the navigation controller stack) later in the course.
  16. Our detail view is pretty ugly We’ll fill out our

    detail view in the next lesson with some cool animations and gesture recognizers