Wednesday, October 19, 2011

iOS 5 Storyboard and UITableViews Tutorial

In this tutorial we will:

1. Create a UITableView from scratch.
2. Learn about navigation controllers.
3. Pushing view controllers with storyboard.

Link to Download Entire Project: here

1. Create a new single view based application called "StoryboardTutorial".


2. Select the main storyboard file and then select Editor > Embed In > Navigation Controller.
Note: A navigation controller is a way to manage views. Typically you push or pop a view onto a navigation controller. You can think of this a stack.


3. Next drag and drop a UITableView from the right sidebar and place it onto the view controller.


4. Go into the ViewController.h file and add after it says UIViewController. This tells the compiler that your class will conform to the UITableViewDelegate/UITableViewDataSource protocols. A protocol is just a formal way of saying that you will implement all methods that are required so you have a functioning tableview. 




Note: The biggest benefit of declaring the ViewController.h object as such is that now Xcode will autogenerate code and most of your necessary methods you would like to implement in the tableview class. Also note, if the method is not auto-generating you're probably doing something wrong. That's the main way I know I'm doing something wrong, or the program crashes.



5. Go back to the main storyboard file and "Control + Drag" from the tableview to the view controller and select "data source" and "delegate". This is how you tell the tableview where it is going to get it's data from, and if an event (a touch event for example) happens, who should it notify.


6. Now click on a tableview cell and click on the right side bar and go to the fourth panel from the left. In the field Identifier type "Cell". This is label so that you can reuse tableview cells which saves memory.


7. In the ViewController.m file implement these protocol methods.
Note: Philosophically it would make sense that you would needs to implement these methods to have create an actually tableview. You need to know 1) how many rows should you have and 2) what should you label these rows and 3) what to do when a user clicks on it.

Debugging Tip: Manually label the cells at first to make sure you made your connections correctly. I once spent three hours of my life trying to figure out why my tableview was empty. Save yourself the misery.



After you implement those methods it should look like this. I changed the table to states because I wanted to later show how to alphabetize a list of states.


8. Go back to the main storyboard file and drag and drop a View Controller object, and in the right panel type "detail" and the Identifier. This will be the detail view controller to push another view onto the view controller.


9. Go to File > New File > UIViewController Subclass and then create a UIViewController subclass called "DetailViewController" and unselect use XIB for interface.



10. Go back to the main storyboard file and in the right panel change the class of your newest view controller to be the "DetailViewController" that you just created.


11. Now go back to the ViewController.h file and then add "#import DetailViewController.h" at the top of your file. Then edit the two methods like below.

Note: What's new in iOS 5 is the storyboard and transitioning from one view to the other. Instead of having lots of Xib files, you replace them with views all placed into one storyboard file. This makes life much easier for the developer. So seen below is how you create a view controller object in storyboard.
"[self.storyboard instantiateViewControllerWithIdentifier:@"detail"" tells the computer to create the view that I called "detail" in the storyboard file, and then the "[self.navigationController pushViewController:detail animated:YES]" then pushes the view onto the view stack, so the visible view is then the current view.

Note: The tableview height method is self-explanatory. For some reason I had a hard time finding that method when I first started so I thought I would include it here for people to play with.


If everything is working correctly you should see these two screens if you select onto a tableview cell.

Note: In keeping with the states them I added two label to the detail view to work with later on.




12. Now if you can add a background image to the tableview cell by editing the cellForRowAtIndexPath method. Here I have a 320x65 pixel image called "LightGrey.png" which I'll include in the project at the end of the tutorial. Adding a background image really adds vibrance to your tableview.





13. Create a datasource by editing your ViewController.h and ViewController.m files so they match the ones below.
















Then make these changes in the ViewController.m file:



Changes:
1) @synthesize states and datasource.
2) In the viewDidLoad method call the function setupArray;
3) Create the datasource in setupArray;
4) Change the text label so that it reads from the datasource.

14. Edit the DetailViewController.h file so it matches the one below.



Then @synthesize them at the top of the DetailViewController.m file.


Then edit the viewDidLoad method.


15. Go back to the main storyboard file and select the detail view controller and "Control + Drag" from the DetailViewController to each of the UILabels and connect them to the appropriate outlet. 








16. Then go back to the ViewController.m file and edit the didSelectRowAtIndexPathMethod so it matches the following. This is just passing data from one view to the other.

So now when you select the state, the detail view will show the state's name and the state capital.








30 comments:

Morten G said...

Very nice tutorial. I notice that the Live Bytes in instruments increases when I switch between table and item view. Not many bytes but over time, it might be a problem.

Is it normal that the Live Bytes grows, when entering the same item View over and over again.

/Morten

Morten G said...
This comment has been removed by the author.
Hdim said...

Hi! Thanks for this tuto, I have a problem, when I am at step 4 and when I add UITableViewDataSource / Delegate, nothing is changed in my .m file..... Is it normal..?

Hdim said...
This comment has been removed by the author.
Avijit Paul said...

Thank you very much. I was new to App development and started with ios5 which made me crazy as all the tutorials are for 4 and I could not get the tableview done initially.

and to @Hdim, I had the same problem and I still have the same problem. Besides it crashed first for me.

After trying many time I closed Xcode and redid from start and that time it worked. It still did not fill up the .m file but at least when I started to type it started to show the code hints.

The Rob Brennan said...

Thanks for such an awesome blog post! I thought I was running into a personal wall when dealing with viewing detail from a chosen table, but this makes perfect sense. =)

sunil786 said...

Thanks for such a nice tutorial. very helpful!

Ole said...

Thanks for a perfect tutorial! I am completely new to iOS programming, but this one gave me a flying start. However, I have a couple of questions:
1. How can I arrange the values in the array either by wish or alphabetically?

2. When a cell in the table is pushed, it gets the usual hover effect before switching view. But when I go back to the table view again, the hover effect is still present on the last item I pushed. This happened not just on my app, but alsp when i downloaded your code.

I really hope you have an answer to these questions because they have been driving me mad the last qouple of days.

Again, thank you for the guide!

Technical Blogs said...

Helped with wiring storyboard's tableview and UIView. Not sure how much time I would spent trying to figure that out without this post. Thanks.

Josan said...

Very nice tutorial, but I have a problem, I want to access the property tableview to reload the new data in the table and not get it.I want do this:

- (void)playerDetailsViewController:
(PlayerDetailsViewController *)controller
didAddPlayer:(Player *)player
{
[self.players addObject:player];
NSIndexPath *indexPath =
[NSIndexPath indexPathForRow:[self.players count] - 1
inSection:0];
[self.tableView insertRowsAtIndexPaths:
[NSArray arrayWithObject:indexPath]
withRowAnimation:UITableViewRowAnimationAutomatic];
[self dismissViewControllerAnimated:YES completion:nil];
}

But I don't know how to do "self.tableVew".

Can you help me, please?

Raspés said...

Gracias por el tutorial. Excelentes imágenes y explicación. Ojalá puedas seguir haciendo mas!!!

David Jiang said...

How would I reload the data in the UITableView?

Maui Perspectives said...
This comment has been removed by the author.
Maui Perspectives said...

Great tutorial. How would I have to modify the .h and .m files of the Detail View Controller if I wish to pass a single image from an array in the View Controller? I have tried many ways and I am getting crashes or errors.

Sudarshan Shetty said...

check it out http://aboutios.blogspot.com/

Bear said...

Is it possible to have two segues from a table cell? When I click a cell I want to navigate to ViewController A or VIewControlleB. It won't let me drag two segues?

imp said...

Thanks for this Very useful tutorial.

David.Dong said...

Thats awsome
I just wondering is it possible that you can add search function to this tutorial?

Lauraine said...

I just searching this kind of things in search engines. My searching was ending here. Keep up your good work. I bookmarked it for general updates.
html5 video player| html5 player

jmin said...

If it crashes, use this

datasource = [[NSMutableArray alloc] initWithArray:[states allKeys]];


And be sure that the datasource is released in dealloc method.

that one Ginger said...

Great tutorial. Bookmarked this blog, I'm hoping it'll be of more help to me in the future! Keep the tutorials coming!

Sandy Shaw said...

I like your blog post. Keep on writing this type of great stuff. I'll make sure to follow up on your blog in the future.
Establishing Serial Point-to-Point Connection

GalaxyGamesKeith said...

Thank you for putting this up here. Was missing a simple step and needed someone to walk me through it quickly. Much appreciated!

Chanchal Raj said...
This comment has been removed by the author.
Technology Blog said...

If anyone has suggestions on what they want me to talk about I will write more in the future.

vandit mehta said...

I Tried it but it gives me warning in Main.m file as 'Signal SIGABRT' and i can not solve it..
any suggestions about how i can solve it..
Thanks in advance..

david Rice said...

Thank you so much for writing a lot of this good information! I am looking forward to reading more.

Dissertation Writing | buy dissertation proposal

alex said...

This is really awesome blog and the information is more awesome and great to see!
Essay Examples

sundeep jan said...

hi sir with storyboard i have taken tableview in that i can set background image for that can u help me

Hamza Shahzad Shahzad said...

Splendid work
Cereb