Unlimited WordPress themes, graphics, videos & courses! Unlimited asset downloads! From $16.50/m
  • Overview
  • Transcript

4.2 Presenting the Articles

Now that we can populate our list view with the feeds that we've been able to successfully parse and in this case just the hard coated feed that we've grabbed here, but we'll go ahead and modify that in an upcoming lesson. We wanna be able to know when we've selected one of our vi, one of our feeds and then transition or segue over to the articles table view controller and present the articles in a very nice way that the user can then click on again. So, we're gonna do that by scrolling down the very bottom and utilizing the segue, which we talked about in a previous lesson. So, before we can really do that, we should set up our articles table view controller in a similar way that we did with our feeds. So we're gonna start off by coming up, and we're gonna create a property here at the top that's going to contain an array of articles. And this array of articles is actually going to get populated through the segue process that we're gonna do when we actually notice that there's somebody clicking on or tapping on one of the feeds. So we're going to create an articles array that's gonna be used when we're actually doing all of the table, the UITableView stuff similarly to how we did previously for the feeds. So we're going to specify that's there's only gonna be one section here, just like we did before. And then the number of rows in the section is going to be equal to articles.count again, so we're gonna return that. And then once again we need to implement our cellForRowAtIndexPath. Again, now this is gonna be similar to the process we did for the feeds, but ever so slightly different. So we'll go ahead and head back over to our story board, and this time we're gonna come over here to our articles and we'll go ahead and select this prototype. Once again, we need to specify a reuse identifier, so we'll call this an Article cell. But, do notice that there's a slight difference here in that for the feeds, we're only showing the title. But for articles, we can show both the title and a subtitle. So, we can put two pieces of data on that particular cell. So, that's what we're going to utilize. So, let's come back to our articles view controller here. Once again, we are dq-ing a reusable cell with an identivier, identifier which we have to specify here. So we're gonna say this is an article cell, just like we specified within our storyboard. So now we have that cell to access but we need to get the corresponding article that is showing up in the tableView so we can do that by grabbing the appropriate article, so we'll make this a constant, so we'll say we want to grab an ArticleModel and we're going to initialize it to Articles. And then we're going to grab, once again, the indexPath.row so we can get that associated article for the particular row that's showing up on the screen, in this case the one that shows up at that particular index path. So once we have that we can now say that the cell, and once again we're gonna grab the text label and remember that this is an optional, just like we saw in the feed, so we're gonna have to actually use the exclamation point to say give me the text label, so I can get access to the text property and then set that to the article.title. So that should take care of that. And then we're going to specify that, so once, when we use the particular type of prototype that we're using for this cell, we have a textLabel as well as a detailTextLabel. So I can play with both of these and set both of their text. And in this case we're just gonna stick down to the bottom here, the pubDates, so we can see when this particular article was actually published. And then we'll return the cell and we'll just move on, so that should be enough for this, but now we actually have to populate the articles array here, so how are we gonna do that? Well once again, we are gonna head back to our FeedsTableViewController and we can do our, our segue magic here, like we talked about in a previous lesson. So, we're gonna get rid of these comments. So, once again a best practice here would say that I should come over here. And I should spec, or I should select this particular segue, and make sure that I'm using this particular identifier and check for it, cuz remember we could have multiple segues with multiple identifiers here. We only wanna do this particular operation when our identifiers equal to ShowArticles. So just to make sure we're kind of using that as a pattern here, we'll go ahead and make sure we'll check to see if our segway.identifier. So we'll check that property is equal to ShowArticles and if that is truly the case then we are going to let, and we are gonna go ahead and grab the, an instance of our viewController, so we'll say viewController. And this is actually going to be an instance of an ArticlesTableViewController. We're going to initialize that to our segue. Now, this is our destination, remember, so we wanna get our destinationViewController. But in this case, we're gonna have to cast it because it's just gonna be a UIViewController, so it's not gonna know about our articles property on the ArticlesTableViewController. So that, that's the reason we have to actually cast that to another type. So once we've done that, we need to figure out which one of our feeds has been selected. Now in our case, we only have one at this particular time, but we're gonna be able to add functionality and to add more than just this one. So let's just assume that there could be multiple feed models within this array. So, when we were doing the selection bit on here for the cellForRowAtIndexPath, we have this indexPath here, so we know which one was actually selected. But how do we do that when we're talking about a selection within a segue? Well, there's another helper method that we can use, so we're going to kind of fake this a little bit. We're gonna create an indexPath, and the way that we're gonna do that is, because we are a UITableViewController, we can reference ourself and we can go into our TableView property. And we can use a method called indexPathForSelectedRow. And this is going to give us an indexPath for the particular row that was selected and once again this is going to provide a return an optional so I need to make sure that I'm getting the actual indexPath. So now that I have that, I can now get the fee that I have selected so I can go ahead and look into feeds and use my indexPath.row. So now I have the feed and now I simply need to take that viewController and I need to set its articles property equal to feed.articles. We'll go ahead and save that. Now, we have now tapped into being able to select a feed. Once we've done that, we check to make sure that we are working with the appropriate segue. And if we are, we're gonna grab that destinationViewController which happens to be an ArticlesTableViewController. Then we're going to get which of the rows within our table view was selected. We're gonna get the appropriate feed for that particular row. And then we're going to set the destination viewController.articles tableViewController property of articles that we just created over here, on this guy right here. And we're gonna set it to the articles associated with that feed. So now, once we get over here, we should be populating all the cells with all the articles that were there. So I'll go ahead and save this in build just to make sure we haven't fat-fingered anything. Now if I run this, once our simulator pops up, we should once again see, here we've parsed it again, News and Updates- Apple Developer. And If I tap on this guy, we can now see all the feeds associated with that RSS. We can see all the articles associated with that RSS feed populated within articles. Now the next step will finally be to say if I tap on one of these, I should come over and be able to see what's happening or actually see that actual article within that web view so I can read the full article.

Back to the top