Tutorial details

Developing iOS Apps Using Swift Tutorial | App Code for Sale | Preview

Developing iOS Apps Using Swift Tutorial - Part 4 – Adding Interactions | iOS Tutorial

How to see the name and price of any app clicked show up in a popup window.

Overview PAGE TOP

In parts 1, 2, and 3 we went over some basics of Swift, and set up a simple example project that creates a Table View and a puts some API results from iTunes inside of them. If you haven’t read that yet, check out Part 1, Part 2, and Part 3.

Make stuff happen! PAGE TOP

Okay, so first things first there’s one more thing we need to clean up. The Table View Cell thats being created in the delegate function cellForRowAtIndexPath is not the most efficient way to create cells. In Obj-C we always wanted to use dequeueReusableCellWithIdentifier in order to get a cell out of memory if one is available, rather than creating a new one every time a cell is rendered. This helps make scrolling fast, and keeps the memory usage down.

So, in our SearchResultsViewcontroller.swift file, we are going to replace the instantiation of the cell with the following:

let kCellIdentifier: String = "SearchResultCell"<br />var cell: UITableViewCell = tableView.dequeueReusableCellWithIdentifier(kCellIdentifier) as UITableViewCell<br />if cell == nil {<br />&nbsp; &nbsp;cell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: kCellIdentifier)<br />}

What this is going to do is give us an already instantiated cell in the first line. The second line checks to see if there was actually a cell set, and if not (if it’s nil), then we instantiate one as we did before. For some reason or another this code crashes if we don’t also specify the cell as a prototype cell in our storyboard, and set it’s reuse identifier to SearchResultCell. So open up your storyboard and select the tableview, change the number of ‘prototype cells’ to 1, and then click in to the cell. In the attributes inspector change the type to “Subtitle” and type in the identifier as “SearchResultCell”.

Run the app, and once again we have the exact same result, except this time it’s actually memory efficient and uses best practices!

Making the cells ACTUALLY do something PAGE TOP

Okay, now we have one more delegate function that UITableView is going to call on our delegate class, SearchResultsViewController. This one will look like this:

func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!)

Whenever a cell is tapped, this function will run. So let’s grab out the iTunes data for the row that was clicked by accessing the index of the array set by indexPath.row, the integer id of the row clicked. And then let’s display the same info in a popup.

Add your didSelectRowAtIndexPath method like so:

func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) {<br />&nbsp; &nbsp;// Get the row data for the selected row<br />&nbsp; &nbsp;var rowData: NSDictionary = self.tableData[indexPath.row] as NSDictionary

&nbsp; &nbsp;var name: String = rowData["trackName"] as String<br />&nbsp; &nbsp;var formattedPrice: String = rowData["formattedPrice"] as String

&nbsp; &nbsp;var alert: UIAlertView = UIAlertView()<br />&nbsp; &nbsp;alert.title = name<br />&nbsp; &nbsp;alert.message = formattedPrice<br />&nbsp; &nbsp;alert.addButtonWithTitle("Ok")<br />&nbsp; &nbsp;alert.show()<br />}

Here, we’re setting rowData to the value of whatever the array object at the selected index is to first get the information in to rowData. Then, we set the name and formattedPrice variables based on the rowData dictionary. Next, we instantiate an UIAlertView object, and set it’s title, message, and add a cancel button. Finally we show the alert with alert.show()

Give it a run and you should now be able to see the name and price of any app clicked show up in a popup window. Cool right?

The full code for Part 4 is available here.

Or as a zip file here: Download.

Part 5 focuses on speeding up our Table View.

Reference PAGE TOP


0 Comments Leave a comment

Please login in order to leave a comment.

Newest first

to your Chupamobile Account.

The Easiest way to Launch your next App or Game.

Join Chupamobile and get instant access to thousands of ready made App and Game Templates.

Creating an account means you’re okay with Chupamobile’s Terms of Service and Privacy Policy.