{"id":628,"date":"2014-06-03T11:17:02","date_gmt":"2014-06-03T17:17:02","guid":{"rendered":"http:\/\/jamesonquave.com\/blog\/?p=628"},"modified":"2015-04-16T14:27:29","modified_gmt":"2015-04-16T20:27:29","slug":"developing-ios-apps-using-swift-part-4-adding-interactions","status":"publish","type":"post","link":"https:\/\/jamesonquave.com\/blog\/developing-ios-apps-using-swift-part-4-adding-interactions\/","title":{"rendered":"Developing iOS Apps Using Swift Part 4 \u2013 Adding Interactions"},"content":{"rendered":"<p><strong><i>This section completely updated to reflect changes in Xcode 6.3, as of April 16, 2015<\/i><\/strong><\/b><\/p>\n<p>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&#8217;t read that yet, check out <a title=\"Developing iOS Apps Using Swift Tutorial Part 1\" href=\"http:\/\/jamesonquave.com\/blog\/developing-ios-apps-using-swift-tutorial\/\">Part 1<\/a>,\u00a0<a title=\"Developing iOS Apps Using Swift Tutorial Part 2\" href=\"http:\/\/jamesonquave.com\/blog\/developing-ios-apps-using-swift-tutorial-part-2\/\">Part 2<\/a>, and <a title=\"Developing iOS Apps Using Swift Part 3 \u2013 Best Practices\" href=\"http:\/\/jamesonquave.com\/blog\/developing-ios-apps-using-swift-part-3-best-practices\/\">Part 3<\/a>. If you enjoy these tutorials, I&#8217;m also working on a book full of quality content for Swift developers, and it&#8217;s available for <a href='http:\/\/jamesonquave.com\/swiftebook'>Pre-Order now<\/a>.<\/p>\n<p><strong>Loading cells from a storyboard prototype<\/strong><\/p>\n<p>Okay, so first there is one more thing we need to clean up. The Table View Cell thats being created in the delegate function\u00a0cellForRowAtIndexPath is not the most efficient way to create cells. In iOS we always want to use\u00a0dequeueReusableCellWithIdentifier 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.<\/p>\n<p>First, we are going to be referring to this cell by a storyboard prototype, which in code is represented by a string.<br \/>\nSo, near the top of the SearchResultsViewController class, add the following line:<\/p>\n<pre class=\"brush: js;\">\r\nlet kCellIdentifier: String = \"SearchResultCell\"\r\n<\/pre>\n<p>Next, we need to do some more storyboard modifications.<br \/>\nIn order for Swift to know what a SearchResultCell is, we need to specify the cell as a prototype cell in our storyboard, and set it&#8217;s reuse identifier to SearchResultCell. So open up your storyboard and select the tableview, change the number of &#8216;prototype cells&#8217; to 1.<\/p>\n<p><img src='http:\/\/jamesonquave.com\/tutImg\/AddPrototypeCells.png' \/><\/p>\n<p>Next, we need to click the newly added prototype cell to select it in the attributes editor panel.<br \/>\nAfter you&#8217;ve selected it, in the attributes inspector change the Style to &#8220;Subtitle&#8221; and type in the identifier as &#8220;SearchResultCell&#8221;.<\/p>\n<p><img src='http:\/\/jamesonquave.com\/tutImg\/CellIdentifier.png' \/><\/p>\n<p>Finally, in our SearchResultsViewcontroller.swift file, we are going to replace the instantiation of the cell with the following:<\/p>\n<pre class=\"brush: js;\">\r\nlet cell: UITableViewCell = tableView.dequeueReusableCellWithIdentifier(kCellIdentifier) as! UITableViewCell\r\n<\/pre>\n<p>What this is going to do is give us an already instantiated cell.<\/p>\n<p>Run the app, and once again we have the exact same result, except now we&#8217;re loading the cell from a storyboard prototype, which allows us to do all sorts of cool stuff with it.<\/p>\n<p><strong>Making the cells do something!<\/strong><\/p>\n<p>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:<\/p>\n<pre class=\"brush: js;\">func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)<\/pre>\n<p>Whenever a cell is tapped, this function will run. So let&#8217;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&#8217;s display the same info in a popup.<\/p>\n<p>Add your didSelectRowAtIndexPath method like so:<\/p>\n<pre class=\"brush: js;\">\r\nfunc tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {\r\n    \/\/ Get the row data for the selected row\r\n    if let rowData = self.tableData[indexPath.row] as? NSDictionary,\r\n        \/\/ Get the name of the track for this row\r\n        name = rowData[\"trackName\"] as? String,\r\n        \/\/ Get the price of the track on this row\r\n        formattedPrice = rowData[\"formattedPrice\"] as? String {\r\n            let alert = UIAlertController(title: name, message: formattedPrice, preferredStyle: .Alert)\r\n            alert.addAction(UIAlertAction(title: \"Ok\", style: .Default, handler: nil))\r\n            self.presentViewController(alert, animated: true, completion: nil)\r\n    }\r\n}\r\n<\/pre>\n<p>Here, we&#8217;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 UIAlertController object, and set it&#8217;s title, message, and add an &#8220;Ok&#8221; button to dismiss the alert. Finally we show the alert with self.presentViewController()<\/p>\n<p>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?<\/p>\n<p>The full code for Part 4 is available <a href=\"https:\/\/github.com\/jquave\/Swift-Tutorial\/tree\/Part4\">here<\/a>.<br \/>\n<a href='http:\/\/jamesonquave.com\/blog\/developing-ios-apps-using-swift-part-5-async-image-loading-and-caching\/'>Part 5<\/a> focuses on speeding up our Table View. To get updates on this tutorial in your email, <a title=\"Sign Up\" href=\"http:\/\/jamesonquave.us6.list-manage.com\/subscribe\/post?u=1d2576bf288fe2fd7fa71bd20&amp;id=6c787ed58a\">subscribe here<\/a>.<\/p>\n<p><a href='http:\/\/jamesonquave.com\/blog\/developing-ios-apps-using-swift-part-5-async-image-loading-and-caching\/'>Go to Part 5 Now -><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This section completely updated to reflect changes in Xcode 6.3, as of April 16, 2015 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&#8217;t read&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_links_to":"","_links_to_target":""},"categories":[26,10,32],"tags":[15,34,33],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v19.13 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Developing iOS Apps Using Swift Part 4 \u2013 Adding Interactions - Jameson Quave<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jamesonquave.com\/blog\/developing-ios-apps-using-swift-part-4-adding-interactions\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Developing iOS Apps Using Swift Part 4 \u2013 Adding Interactions - Jameson Quave\" \/>\n<meta property=\"og:description\" content=\"This section completely updated to reflect changes in Xcode 6.3, as of April 16, 2015 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&#8217;t read...\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jamesonquave.com\/blog\/developing-ios-apps-using-swift-part-4-adding-interactions\/\" \/>\n<meta property=\"og:site_name\" content=\"Jameson Quave\" \/>\n<meta property=\"article:published_time\" content=\"2014-06-03T17:17:02+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2015-04-16T20:27:29+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/jamesonquave.com\/tutImg\/AddPrototypeCells.png\" \/>\n<meta name=\"author\" content=\"Jameson Quave\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Jameson Quave\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jamesonquave.com\/blog\/developing-ios-apps-using-swift-part-4-adding-interactions\/\",\"url\":\"https:\/\/jamesonquave.com\/blog\/developing-ios-apps-using-swift-part-4-adding-interactions\/\",\"name\":\"Developing iOS Apps Using Swift Part 4 \u2013 Adding Interactions - Jameson Quave\",\"isPartOf\":{\"@id\":\"https:\/\/jamesonquave.com\/blog\/#website\"},\"datePublished\":\"2014-06-03T17:17:02+00:00\",\"dateModified\":\"2015-04-16T20:27:29+00:00\",\"author\":{\"@id\":\"https:\/\/jamesonquave.com\/blog\/#\/schema\/person\/db6184f355c7f4e3b876d0f228c2fcfc\"},\"breadcrumb\":{\"@id\":\"https:\/\/jamesonquave.com\/blog\/developing-ios-apps-using-swift-part-4-adding-interactions\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jamesonquave.com\/blog\/developing-ios-apps-using-swift-part-4-adding-interactions\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jamesonquave.com\/blog\/developing-ios-apps-using-swift-part-4-adding-interactions\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jamesonquave.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Developing iOS Apps Using Swift Part 4 \u2013 Adding Interactions\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/jamesonquave.com\/blog\/#website\",\"url\":\"https:\/\/jamesonquave.com\/blog\/\",\"name\":\"Jameson Quave\",\"description\":\"Using computer technology to educate, and improve lives.\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/jamesonquave.com\/blog\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/jamesonquave.com\/blog\/#\/schema\/person\/db6184f355c7f4e3b876d0f228c2fcfc\",\"name\":\"Jameson Quave\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jamesonquave.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/d9786c83345117d560bbeab0e1f26814?s=96&d=retro&r=pg\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/d9786c83345117d560bbeab0e1f26814?s=96&d=retro&r=pg\",\"caption\":\"Jameson Quave\"},\"sameAs\":[\"http:\/\/jamesonquave.com\"],\"url\":\"https:\/\/jamesonquave.com\/blog\/author\/jquave\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Developing iOS Apps Using Swift Part 4 \u2013 Adding Interactions - Jameson Quave","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jamesonquave.com\/blog\/developing-ios-apps-using-swift-part-4-adding-interactions\/","og_locale":"en_US","og_type":"article","og_title":"Developing iOS Apps Using Swift Part 4 \u2013 Adding Interactions - Jameson Quave","og_description":"This section completely updated to reflect changes in Xcode 6.3, as of April 16, 2015 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&#8217;t read...","og_url":"https:\/\/jamesonquave.com\/blog\/developing-ios-apps-using-swift-part-4-adding-interactions\/","og_site_name":"Jameson Quave","article_published_time":"2014-06-03T17:17:02+00:00","article_modified_time":"2015-04-16T20:27:29+00:00","og_image":[{"url":"http:\/\/jamesonquave.com\/tutImg\/AddPrototypeCells.png"}],"author":"Jameson Quave","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Jameson Quave","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/jamesonquave.com\/blog\/developing-ios-apps-using-swift-part-4-adding-interactions\/","url":"https:\/\/jamesonquave.com\/blog\/developing-ios-apps-using-swift-part-4-adding-interactions\/","name":"Developing iOS Apps Using Swift Part 4 \u2013 Adding Interactions - Jameson Quave","isPartOf":{"@id":"https:\/\/jamesonquave.com\/blog\/#website"},"datePublished":"2014-06-03T17:17:02+00:00","dateModified":"2015-04-16T20:27:29+00:00","author":{"@id":"https:\/\/jamesonquave.com\/blog\/#\/schema\/person\/db6184f355c7f4e3b876d0f228c2fcfc"},"breadcrumb":{"@id":"https:\/\/jamesonquave.com\/blog\/developing-ios-apps-using-swift-part-4-adding-interactions\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jamesonquave.com\/blog\/developing-ios-apps-using-swift-part-4-adding-interactions\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jamesonquave.com\/blog\/developing-ios-apps-using-swift-part-4-adding-interactions\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jamesonquave.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Developing iOS Apps Using Swift Part 4 \u2013 Adding Interactions"}]},{"@type":"WebSite","@id":"https:\/\/jamesonquave.com\/blog\/#website","url":"https:\/\/jamesonquave.com\/blog\/","name":"Jameson Quave","description":"Using computer technology to educate, and improve lives.","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jamesonquave.com\/blog\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/jamesonquave.com\/blog\/#\/schema\/person\/db6184f355c7f4e3b876d0f228c2fcfc","name":"Jameson Quave","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jamesonquave.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/d9786c83345117d560bbeab0e1f26814?s=96&d=retro&r=pg","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/d9786c83345117d560bbeab0e1f26814?s=96&d=retro&r=pg","caption":"Jameson Quave"},"sameAs":["http:\/\/jamesonquave.com"],"url":"https:\/\/jamesonquave.com\/blog\/author\/jquave\/"}]}},"_links":{"self":[{"href":"https:\/\/jamesonquave.com\/blog\/wp-json\/wp\/v2\/posts\/628"}],"collection":[{"href":"https:\/\/jamesonquave.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jamesonquave.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jamesonquave.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jamesonquave.com\/blog\/wp-json\/wp\/v2\/comments?post=628"}],"version-history":[{"count":24,"href":"https:\/\/jamesonquave.com\/blog\/wp-json\/wp\/v2\/posts\/628\/revisions"}],"predecessor-version":[{"id":1766,"href":"https:\/\/jamesonquave.com\/blog\/wp-json\/wp\/v2\/posts\/628\/revisions\/1766"}],"wp:attachment":[{"href":"https:\/\/jamesonquave.com\/blog\/wp-json\/wp\/v2\/media?parent=628"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jamesonquave.com\/blog\/wp-json\/wp\/v2\/categories?post=628"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jamesonquave.com\/blog\/wp-json\/wp\/v2\/tags?post=628"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}