Swift How-To: Setting up a Table View

Think of this screencast as the “Hello World” of creating a view controller with a table view. I cover organizing things in the Storyboard, Auto Layout constraints, and the implementation of the data source protocol methods for a table view.


Screencast Transcript


Hi, I’m Andrew from andrewcbancroft.com.

I use table views fairly often in my examples, so I’ve decided to create a short walkthrough of how to set up a table view in Swift.

Think of this as the “Hello World” of creating a view controller with a table view.


In this walkthrough, I’m using Xcode 6.3.1, which has me writing Swift 1.2 syntax in a couple of spots.


To get started, I’m going to drop into the search bar of the Object Library, which is in the Utilities Pane to hunt for a table view.

Once I’ve narrowed the results and located the Table View control, I’ll click and drag one onto my Storyboard scene.


With the table view in place, filling the entire scene, I’ll control + click and drag up to the yellow View Controller icon to wire up my the table view’s dataSource… and delegate properties.


Now I need to connect the table view to my View Controller source file. To bring up the assistant editor, I’ll option+click the ViewController.swift file in the Project Navigator.


With the table view and the source side by side, once again, I’ll control + click the table view, and drag over to the ViewController source file to create an IBOutlet property for my table view.


Next up is to specify that my ViewController class will conform to the UITableViewDataSource protocol… and the UITableViewDelegate protocols.


Of course, at this point, I haven’t actually implemented any of the required protocol methods, so I get a compiler error saying so.

I’ve written up a handy UITableViewDataSource cheat sheet over at my website (andrewcbancroft.com). I’ll go grab that code and paste it into my View Controller.


Here I am at andrewcbancroft.com – I’ve searched for “UITableViewDataSource” and located my cheet sheet.


Scrolling down, I find the code I need, switch the code snippet plugin to “raw” mode, select everything, and copy.


Switching back over to my Xcode window, I paste the code snippet in.


As I mentioned in the beginning, this walkthrough can be thought of as the “Hello World” of table view setup. With that in mind, for now, I’ll create a dataSourceArray with some dummy data in it, just to get things compiling and displaying data.


Now that I’ve got the data source geared to go, I need some cells to display data in.

For this task, I’ll close the assistant editor…

Next, I’ll click to open the document outline. I find that working with the view hierarchy is much easier to manipulate using this outline, rather than clicking to try and select the right thing in the Storyboard Scene.


I’ll select the table view, and open the Utilities pane to get access to the Attributes inspector.


I’m going to configure this table view to have one kind of prototype cell.


Increasing the Prototype Cells count added a new node to the document outline’s view hierarchy.

I’ll expand the Table View and click on the Table View Cell to do a bit more configuration.


I’ll set the Style to Basic…

Then I’ll give it an identitifer of “BasicCell”.

I need this “BasicCell” string for one of my other data source proocol methods, so I’ll select it, and copy it.


Back in my ViewController.swift source file, I’ll paste that “BasicCell” string as the argument to dequeReusableCellWithIdentifier.


The last step to get things wired up is to set the cell’s textLabel.text property.

I’ll use the dataSourceArray’s values here.

Simply indexing into the array at the indexPath.row value that’s currently being rendered in the table view will grab the string out of the array and assign it to the textLabel.text property of the cell.


When I run the app and observe the table view’s behavior, I’m not quite satisfied with it. The status bar overlaps the rows and makes the text hard to read.

Additionally, the table view doesn’t extend all the way to the bottom when I run it in the iPhone 6 sized simulator.


I’ll stop the app and make an adjustment in the Storyboard.


Selecting the table view from the document outline will allow me to grab the top edge and drag the hight downward slowly until it “snaps” into place where the bottom of the status bar would appear to be.


Next, I’ll set some constraints. Within the document outline, I’ll control + click from the Table View to the View and set constraints for “Top Space to Top Layout Guide”, Center Horizontally, Center Vertically, and Equal Widths.


Running the app one more time gives me the behavior I want!


Thanks for watching – I have other resources r elated to Swift and iOS development at andrewcbancroft.com, and you can find me on Twitter: @andrewcbancroft

comments powered by Disqus