A Table View data source is the object that provides information to fill the table view. A Table View data source must be created in code, and includes such information as how many rows are in the table view, how many sections there are, and what exactly goes into each table view cell in the table view in iOS.
- [Voiceover] Now the Table View needs two things. The first one, and the most important, is a datasource. We need to say what we're going to put inside the Table View. Now just like with the Picker View where we had to manually set a datasource, we're going to have to do that with the Table View as well. So we need to connect to this object to a datasource object and then have that object provide the data to populate the Table View. So we can't just add the Table View data inside of Interface Builder. The other thing a Table View needs is a delegate.
Now the delegate doesn't say anything about what data's inside the Table View. It says what happens when someone taps or clicks on a row in Table View. It handles Table View events. So for now we're just going to focus on the datasource of the Table View. So with the Table View selected I'm going to head over to the connections inspector. Now you should see datasource and delegate under outlets. If not, just make sure that you have the table view selected and it should be there. Now we want to connect this datasource to our View Controller.
The easiest way to that is just click and drag from that circle over to the View Controller, that little yellow circle with a white square inside of it. Release the mouse button, and then with the Table View selected you should see that the outlet is then set up. So that's step one. Step two is to go over to the View Controller class. And then we need to say that this class can be a UITableView datasource. And just as we looked at with the Picker View earlier, we have to set that inside of the class declaration.
So after we have the sub-classing UIViewController declaration, we type a comma and a space and then UITableViewDataSource, notice there's also TableViewDelegate, so we'll look at that later on. So we want DataSource here, and remember that this defines a protocol that our class is supposed to implement. A protocol is just a set of methods that our class needs to handle if we are using that particular protocol, in this case UITableVIewDataSource.
So to find out the methods that are required, you can look in the Quick Help section. And you can read all of this information. This talks about both the UITableViewDataSource and the UITableViewDelegate protocol. But for now all we care about is the DataSource. So what I'm going to click on in this reference here that says UITableViewDataSource protocol reference. And when I click on that, I'm taken to the documentation, and it explains what methods are required, which methods are optional.
And if we scroll down we can read information about them. So this one's required, tableViewcellForRowAtIndexPath, and this actually returns the cell for a certain row. So we need to create a tableView cell and then return it. We can put text in if we want or customize it however we want. So this is a required method, tableViewcellforRowAtIndexPath. If you scroll down, you'll also see number of sections in tableView, and then tableView number of rows in section.
This one is also required. So those are the two that we're going to look at right now. Number of rows in section, and cellForRowAtIndexPath. Now there's this other one here, number of sections in tableView. Sections are like the groups, so you can have one tableView with multiple groups of rows. Now this one is not required but if you wanted to have more than one section, you can put that here, handle that method and return the number of sections that you want in your tableView. So for more information you can feel free to pause the movie now and look through this protocol reference.
I'm going to close this and head on back to Xcode. Now the reason why we're getting this error is because we haven't yet implemented the required methods. So let's go below everything else in our code, and we're going to handle tableView, cellForRowAtIndexPath, and that's this one right here. And I'm not going to return anything yet, and that's going to give me an error, because I need to return something. And then I'm going to handle tableView, numberOfRowsInSection. And right now we need to return an int, inside number of rows in section.
That's how many rows are inside the tableView. So you can just return and then pick a number here. I'll just say 5. And now let's go inside cellForRowAtIndexPath, so in here we need to create and return a UITableView cell. So I can define a cell, set it equal to a new instance of UITableViewCell. Run the constructor, and there's our cell. And then we can change some kind of property on it, so be default the tableView cell has a label, and that label is inside of the text label property.
If you look at the code ending you'll see that this is a UI label, just like the UI labels that we've worked with up to this point in the course. So we can set that label's text property, equal to whatever you want. Put some text in a string there. And then of course we need to return that cell, so return cell, and that's it. So we're going to have five rows and each row is just going to say "cell." Well let's see what we get when we test this out. So we get five cells right there and we can kind of scroll a little bit.
If we click on them, they get selected but nothing happens, because we need to define that, we haven't defined it yet. But we do have a tableView with five rows in it. And we've created cells and returned them in cellForRowAtIndexPath. So remember, when you're going to use a tableView you need to set the DataSource, and an easy way to do that, is by selecting the tableView in Interface Builder and setting the DataSource outlet in the connections inspector at the top of the utility area. Once you've done that, you need to declare that your class implements the UITableViewDataSource protocol, and then you need to handle at least these two methods, tableView, cellForRowAtIndexPath, where you create the cell and return it, and tableView, numberOfRowsInSection, where you return how many tableView rows there should be in your table.
Interested in developing for Apple Watch or Apple TV? Get a sneak peek in Chapter 9. The course wraps up with a look at the submission process for the App Store.
- Installing Xcode and the iOS SDK
- Creating a simple iOS app
- Creating a basic interaction with a button
- Choosing an object as a first responder
- Creating a user interface with Interface Builder and Auto Layout
- Working with sliders and progress bars
- Creating views to show images and web pages
- Developing responsive layouts with size classes
- Understanding the iOS architecture
- Loading data in table views
- Transitioning between views
- Developing for the iPad, Apple Watch, and Apple TV
- Submitting apps to the App Store