iOS SDK and SQLite: Building Data-Driven Apps
Illustration by Don Barnett

Creating the item view controller


From:

iOS SDK and SQLite: Building Data-Driven Apps

with Bill Weinman

Video: Creating the item view controller

Before we can get the item data from the network for display, we need to create the table view for the item view controller. Let's start by making a working copy of the BWRSS-XML-start project. I'm going to Option drag this and rename it to BWRSS-XML-02. Now inside that folder, there is a file called BWRSS.xcodeproject, I'm going to double-click on that Xcode project file, and that will open the project in Xcode. We'll expand a couple of these disclosure triangles, so we can see our source code, and I want to select this BWRSS Group, which looks like a folder in its icon there.
Expand all | Collapse all
  1. 8m 29s
    1. Welcome
      57s
    2. Exercise files
      2m 17s
    3. Course overview
      3m 4s
    4. Application overview
      2m 11s
  2. 14m 49s
    1. Prototyping in a testbed
      1m 27s
    2. Building the view controller
      3m 45s
    3. Coding the testbed
      7m 56s
    4. Using the testbed
      1m 41s
  3. 37m 49s
    1. Understanding SQLite in iOS
      1m 41s
    2. Creating an Objective-C interface for SQLite
      9m 57s
    3. Testing the BWDB interface in the sandbox
      7m 1s
    4. Designing a database schema
      8m 7s
    5. Supporting the application with a specific interface
      7m 7s
    6. Using C pointers with automatic reference counting (ARC)
      3m 56s
  4. 21m 18s
    1. Understanding the table view
      1m 33s
    2. Creating the view controller
      6m 39s
    3. Reading from the database
      13m 6s
  5. 33m 50s
    1. Understanding the parsing process
      1m 57s
    2. Creating the item view controller
      12m 25s
    3. Reading data from the internet
      5m 30s
    4. Parsing the feed with NSXMLParser
      8m 2s
    5. Updating the item view with the feed items
      5m 56s
  6. 40m 14s
    1. Understanding the modal view
      1m 47s
    2. Constructing the view controller
      15m 5s
    3. Finding a feed link in a web page
      8m 55s
    4. Parsing the feed with NSXMLParser
      5m 4s
    5. Delegating back to the parent view
      6m 11s
    6. Deleting feeds
      3m 12s
  7. 21m 5s
    1. Creating the web view class
      12m 33s
    2. Coding the web view
      5m 25s
    3. Viewing pages in Safari
      3m 7s
  8. 14m 3s
    1. Understanding the iOS preferences system
      1m 23s
    2. Creating the preferences plist in Xcode
      7m 20s
    3. Reading preferences in your application
      5m 20s
  9. 6m 15s
    1. Adding pull-to-refresh functionality
      2m 34s
    2. Implementing the pull-to-refresh gesture for iOS 6
      3m 41s
  10. 27m 1s
    1. Understanding split view
      1m 4s
    2. Coding the table views
      11m 24s
    3. Implementing the iPad detail view
      6m 35s
    4. Implementing the iPad modal view
      7m 58s
  11. 35s
    1. Goodbye
      35s

Start your free trial now, and begin learning software, business and creative skills—anytime, anywhere—with video instruction from recognized industry experts.

Start Your Free Trial Now
please wait ...
Watch the Online Video Course iOS SDK and SQLite: Building Data-Driven Apps
3h 45m Intermediate Jan 16, 2013

Viewers: in countries Watching now:

The iOS software development kit (SDK) includes the popular SQLite library, a lightweight yet powerful relational database engine that is easily embedded into an application. In this course, Bill Weinman teaches you how to build an RSS reader for iOS devices, integrating XML data and a streamlined interface. He explains how to use the SQLite database, display information in a table view, code view controllers, and create a preferences pane for your app. The resulting application is optimized for all iPhone and iPad displays.

Topics include:
  • Prototyping the app
  • Coding and working with a testbed
  • Creating an Objective-C interface for SQLite
  • Designing a database schema
  • Creating the view controllers
  • Reading and writing to the database
  • Parsing the RSS feed with NSXMLParser
  • Updating the item view with feed items
  • Implementing the pull to refresh gesture for iOS 6
  • Creating a universal application with multiple views
Subject:
Developer
Software:
iOS
Author:
Bill Weinman

Creating the item view controller

Before we can get the item data from the network for display, we need to create the table view for the item view controller. Let's start by making a working copy of the BWRSS-XML-start project. I'm going to Option drag this and rename it to BWRSS-XML-02. Now inside that folder, there is a file called BWRSS.xcodeproject, I'm going to double-click on that Xcode project file, and that will open the project in Xcode. We'll expand a couple of these disclosure triangles, so we can see our source code, and I want to select this BWRSS Group, which looks like a folder in its icon there.

And I'm going to create a new class file. We need to set up our Table View for the items so we do that by creating a class. I can either right click on this and say New File, or I can just come up here in the File menu and select New File. Under iOS and Cocoa Touch, select Objective-C class and press Next. Now, I going to name the class, BWRSSItemsTableViewController, and that's a subclass of UITableViewController. If UITableViewController is not selected there, you want to select it from this drop down box.

We're going to leave both of these items unchecked, we're not targeting for the iPad, and we're not using a NIB File. And I'll select Next and the default location here is in this BWRSS folder, which corresponds to this BWRSS Group over here. We want to make sure that the BWRSS Group is selected, and that it's targeted for the BWRSS Application. So we select Create, and there we now have our class files. I'm just going to drag them up here so that they're in with the other class files.

Now we're going to open the Storyboard and create a segue for our new Table View. So, I'm just going to open this up a little bit, so we can see the storyboard_file name_iPhone_storyboard, and because I have such limited real estate here on the screen, I'm going to zoom out a little bit. You probably won't need to do that, and there's a lot of things that don't work right when it's zoomed out. So don't, if you don't need to. Then here, make sure that the Utilities Pane is selected, and under Objects here, I'm going to grab the Table View Controller, and I'm just going to drop it right about there and then I can adjust it so that it aligns properly. It's not necessary, but it's a good idea.

Now I'm going to zoom back in here, and I'm going to create as segue. So I created a segue by control dragging from one object that can be selected to our New Table View. So, over here you see we have the Feeds Table View. If I scroll down you can see that it's Feeds Table View Controller. And I'm going to Control-click on the Table Cell here and drag over towards the New Table View, and you see that I get this little line, and when I let go of the mouse button, I get this Select a Segue, and I select the Push type of segue, and it creates this segue.

So this here is the segue, and if I click on that, then I can actually edit it. Let me select over here the Attributes Inspector, and I can give the segue an identifier. So, I'm going to call this Segue, SegueToItemsTableView. I am going to press the Return key, because that actually makes this take effect, sometimes it doesn't happen if you type something into a field in Xcode, and you don't press the Return key. So, now our segue is named, and you see its Style is Push, and so we've created the segue.

There are just a couple more of things we want to do to our New Table View. You'll notice down here it says Table View Controller, and so if I select this, and I select this orange circle here, which is the Actual View Controller, and come back over here to the Identity Inspector, you notice that its class is UITableViewController. We want our new sub-class to be the Class for this table view. So if I select this BWRSSItemsTableViewController, you'll notice that now, it says Items Table View Controller. If I select a different controller over here, it will say Items Table View Controller, and that's what you want it to say.

And that way it will be actually be using the code in our BWRSSItemsTableViewController.m and .h file. So you notice that this is grayed out over here a little bit, this icon for our storyboard. I'm going to press Command+S on my keyboard, and that will take that gray away and make it white, to indicate that it's saved. I tend to press the Command+S key a lot, sometimes I might forget to say that I'm doing that. But it's an important habit to get into. Now I'm going to select our Table Cell, and if I come back over here to the Attributes Inspector, under Style I'm going to select Subtitle, make sure that the Accessory says None, and then the reuse Identifier, I am going to type in itemsCell, like that and press the Return key.

You notice over here in our Feeds Table View that the cell has this little disclosure indicator. We actually don't need that. So I'm going to select that, under Accessory in the same pane, I'm going to select None, and you see that that goes away. So now, we're all set up. I am going to press Command+S to Save and come back over here to our ItemsTableViewController.h file, and the first thing I'm going to do in this .h file is I'm going to import our database header. So I'm going to come up here and type #import, and I am going to type RSSDB.h, see I got my code completion there, and Command+S to Save.

Now, when I come back out here to Finder, and you'll notice we've got a few text files here in our Exercise Files folder in the Chapter04 folder. That's because Objective-C tends to be pretty verbose, and we don't want to have to type all this stuff. So I am going to open this header, this is 02-header.text file. In my text editor--I'm using TextWrangler, you can use any text editor as long as it's a plain text editor, a coding text editor and not a Word Processor. I'm going to select these three lines here, press Command+C to Copy, and come back over here into Xcode, and I'm going to just select this one line, here on line 12 and press Command+V.

Now, we have our interface property, property, and end, and that's the way that that should look. Now, press Command+S to Save. Let's go ahead and close our Utilities pane, so this all fits on my little screen here. You can leave that open on yours. I just have limited screen real estate here. We can see that our class is called BWRSSItemsTableViewController. It's a subclass of UITableViewController, and it implements the NSXMLParserDelegate protocol.

We have two properties declared, rssDB and currentFeedID, and those are populated actually from the FeedsTableViewController. We'll see that in a little bit. So, this header file is now complete. I'm going to press Command+S to Save, and we're going to open up the .m file for the ItemsTableViewController. As I scroll down here, you'll notice a couple of these orange lines here, these are warnings. We can just remove this safely. We'll be editing these methods later on in this chapter, but I'm just going to remove the warnings for now. Press Command+S to Save.

I'm going to come back out here to the Finder, and I'm going to open this text file here, -02-methods. This will open up in my text editor, and you see we have quite a few things in here. The first thing we're going to do is select these class extension for private members lines, and I'm going to press Command+C to Copy. And come back over here into Xcode, and I'm just going to select this line right here, which is our interface declaration for a class. And I'm going to press Command+V, and we'll replace that line with what we've copied and pasted from our text file.

So, we have a few variables here that are being used as private members for our class, and we'll talk more about each of them later on. Going back over here to our text editor, I've got some data properties, and again, these are private variables inside our class. I'm going to copy all of those, and I'm going to paste those in right here, and press Command+S to Save. Finally, down here we have this viewWillAppear method.

I'm going to copy that, and I'm going to paste that in right here. Press Command+S to Save. So you notice here in this code, one thing that we do is we check the currentDevice userInterfaceIdiom to see if we're running on an iPad. If we are, we set this private member variable iPadIdiom = YES, and you can see up here in our class extension, we have this BOOL iPadIdiom. So, we can test that later on in the code to see if we're running on an iPad.

One other thing you'll notice here, I'm using this rssDB property and also this feedRecord property. These are populated from the FeedsTableViewController, and we'll get to that in a moment, and we're using that to actually find the current record for this view. So, we select a row on the FeedsViewController, it will bring this ItemsViewController already populated with the items in that feed, and this is how that's done. So, if we go over here into the FeedsTableViewController--make sure this is saved, I'm pressing Command+S again--and we come over here into the FeedsTableViewController, and we select this prepareForSegue. You notice that this is not populated yet.

Come back out here into Finder and open this FeedsTableViewController-02-Support.txt in our text editor and here's our segue delegate for that file. So, I'm going to copy and paste that into Xcode. Just select this entire method here and press Command+V, and we haven't yet imported this header file, so we're getting a couple of errors. We'll take care of that in a moment. You'll notice that this gets called when a segue happens from this view controller.

This is the FeedsTableViewController, and it gets passed this segue object, and that segue object has a destinationViewController, and so I can assign that destinationViewController to a BWItemsTableViewController class object and then I can actually populate ItemsTableViewController.currentFeedID and ItemsTableViewController.rssDB. So if I come up here to the top then I'm just going to import our header file BWRSSItemsTableViewController.h.

Now, when I come back down here to the prepareForSegue, you see those errors have gone away, and we are now assigning our FeedID and our rssDB to the properties in the ItemsTableViewController. I press Command+S to Save this, and we see over in our ItemsTableViewController in ViewWillAppear, this is where we're using those variables. We are also over here setting our row height so that we have enough height to display the things that we want to display. Now, I can just build this.

I'm going to press Command+B to Build. Before I run in the simulator, I want to make sure I've got the iPhone simulator and not the iPad simulator selected, and then I can just press Run, and this will run it in the simulator. There we are. This is the 4-inch Retina iPhone simulator, and this is scaled down because this screen is so small. It doesn't have enough pixels to actually support that. Hopefully you're running on a bigger screen. So, this doesn't have any borders. It doesn't have a Home button, but this is the iPhone simulator. Yours will probably look different, and when I select one of these feeds here, you see it brings up our new ItemsTableViewController, and you can see there's the title that gets populated right here, self.title = self.feedRecord (@"title"), so it's actually reading that from the database based on the current feedID that got passed from the FeedsTableViewController. So, there it is.

That is completely working. I can bring up the lynda blog. Of course, none of these are being populated yet. That will happen in one of our later movies, but we now have a working table view for the items. We're using the segue from the storyboard to set up the ItemsTableView, and now we are ready to fill in the ItemsTableView with some data, and we'll do that in the next lesson.

Find answers to the most frequently asked questions about iOS SDK and SQLite: Building Data-Driven Apps .


Expand all | Collapse all
please wait ...
Q: Why is the RSSDB library in the exercise files different than the one in the videos?

A: The RSSDB library had to be updated to work around a bug in the iOS 7 SDK.

There is a bug in the iOS 7 SDK that prevents the BWDB fast enumeration implementation from working on a device. The symptom is code that runs fine on the emulator, but not on a device. iOS devices use an ARM processor, while the emulator runs on your Mac's Intel processor. This points to the LLVM ARM code generator as the source of the bug. Because the bug appears to be in the LLVM compiler, it may be some time before it is fixed.

As a workaround we have changed the getFeedIDs and getItemIDs methods in the RSSDB library so they don't use Objective C fast enumeration. 

Please note that this same bug also affects some of the BWDB testbed code in Chapter 2. The result is that it will run on the emulator but not on a device.

Q: After upgrading to Xcode 5.1 I get an error that says:

"Used type va_list (aka_builtin_va_list) where arithmetic or pointer type is required"

A: Please download the exercise files again to get the latest version of the BWDB library.
Q: I'm using Xcode 6. Why am I getting error messages with the exercise files?
A: A lot has changed in iOS since this course was released. The author is in the process of rewriting the code and updating the course for iOS 8. In the meantime he has prepared a version of the app that works in iOS 8 and Xcode 6. Download it here:
 
http://ios.bw.org/
 
 
Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

Please wait... please wait ...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.


Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ .

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

* Estimated file size

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.


Mark all as unwatched Cancel

Congratulations

You have completed iOS SDK and SQLite: Building Data-Driven Apps.

Return to your organization's learning portal to continue training, or close this page.


OK
Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member ?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferences from the dropdown menu.

Continue to classic layout Stay on new layout
Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.

Learn more, save more. Upgrade today!

Get our Annual Premium Membership at our best savings yet.

Upgrade to our Annual Premium Membership today and get even more value from your lynda.com subscription:

“In a way, I feel like you are rooting for me. Like you are really invested in my experience, and want me to get as much out of these courses as possible this is the best place to start on your journey to learning new material.”— Nadine H.

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.