In a previous article we learned how to use the UITableView and display some static data.  Today we are going to build on that sample and load the data and display it in the UITableView.

We are going to display the names in the following JSON located here.

First we start by writing the code to download the JSON data from the internet.

The downloadData function uses the NSURLSessionDataTask to asynchronously download the JSON data.  I use the SwiftyJSON library to parse the raw JSON data into a workable JSON object.

After parsing the data we can loop through the array with the following code:

The for loop adds the names from the JSON array into the items array in the ViewController.  After the data has been added to the items array we need to tell the UITableView to reload in order to display the data we downloaded.  The line of code below accomplishes this.

This code uses the Dispatcher code from this previous article in order to call the reloadData function on the UI Thread.

Finally we add the call to download the data to the viewDidLoad function.

 

http://buildsucceeded.com/oldpics/Screen Shot 2014-08-30 at 9.18.31 PM

Above is a screenshot of the updated UI code.

Download the full controller code: ViewController.swift

 

I recently did an example with a UITableView control and I noticed it did not refresh until you force the TableView to scroll.  Then all of a sudden the new items I added to my array would show up.  Some googling led me to this article which explains to use performSelectorOnMainThread in Objective-C like this:

In another article someone showed that you can use some C++ functions to perform this in swift a little nicer like this:

In C# and WPF we use a similar concept called a Dispatcher.  I simplified this a little but and wrote a little Dispatcher function for Swift that makes dispatching code to the main UI thread very clean and simple. Here is the Dispatcher code:

So if you want to solve the problem I had with the UITableView not updating you would simply write this code:

You can download the code here:  Dispatcher.swift

This article describes how to add a MapKit map to your iOS application.  It shows the basics and even centers the map on a Latitude and Longitude using Swift.

The basic steps in the video are:

  1. Create an iOS application using the Swift language.
  2. Add the MapKit framework library to the project.
  3. Add the map to the view.
  4. Make a reference to the MapKip map control in your view model.
  5. Write a little code to center the map.

So I have been developing for iOS using cheats for a really long time and now that Swift is in beta I am going to try and convert over to using that instead of Xamarin and Appcelerator. I will be creating a series of blog posts outlining the things I have learned. Most of what I am discussing will be related to a Microsoft Developer converting to an Apple world.

The first thing you need to do is click Show the assistant editor which is the icon in xcode that looks like a tuxedo.  This displays the view controller code next to the storyboard designer.Main_storyboard_and_1__bash

 

 

Next, you will right click the UI control (in this case, the label that reads “First View”) and drag into the code on the right and drop inside the class definition above the viewDidLoad method.  You will see a blue line while you are dragging.  If you do not see that and your control moves you did not right click.
Main_storyboard_and_1__bash2

 

 

When you release the right button xcode will display a small callout editor window.  This allows you to name the variable that will reference the UI control.  I named mine lblTitle in this example.
Insert_Connection_and_Main_storyboard

Now you will see a variable in your code.  You can use this variable to reference the control.Main_storyboard_—_Edited

To demonstrate using the variable I set the text property of the label control I referenced to the text “Title”.  As you can see, I put this code in the viewDidLoad method.

Main_storyboard_—_Edited2

If you run the code in the simulator you will see the text for that label does in fact change when the view is displayed.
iOS_Simulator_-_iPhone_5s_-_iPhone_5s___iOS_8_0__12A4345d_

This is a very basic example of how to reference a UI component and set a property with swift.

 

 

 

 

 

I am transitioning away from BlogEngine.net to a more robust platform with editor support in both Mac and Windows since I am spending a lot of time in the Mac world now.  I will work on a content importer (if one does not already exist) and will update this post with that information.