Ready to watch this entire course?
Become a member and get unlimited access to the entire skills library of over 4,974 courses, including more Developer and personalized recommendations.Start Your Free Trial Now
- View Offline
- 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
Skill Level Intermediate
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.