Monday, December 29, 2008

UITableView Part III

We may take a break after this look at the UITableView and how to drill-down, but with these three sections on UITableViews down you should have a very strong understanding of how the UITableView Controller works.  So we will open up our code from the last part again and get right into it.

This section will cover how to drill-down.  There are three main things to understand when learning to drill down.
-Handling which row was touched
-Setting something to be an identifier in the next level so we know which one was selected there as well
-Pushing that view onto the stack

The first thing we will have to do is create a new UITableViewController class, so select File -> New and grab a UITableViewController.  I named mine "SecondLevelController."  In the .h of the new file we will add a property of an NSString.  This will be:

NSString *myTitle;


@property (copy, readwrite) NSString *myTitle;

Just as we have in the past.  Also, make sure to synthesize it!

@synthesize myTitle;

Beyond that all we will do for now is make sure viewDidLoad holds:

self.title = myTitle;

That's all for now, since this is just a simple example of this.  We'll go back into the RootViewController class.  Remember to add this up by the other imports:

#import "SecondLevelController.h"

Now, we'll handle the selection of the row.  Remember from before?  There was this class for us:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

Well, we'll be using this to handle the selection, so we can add a few lines of basic code.  Here's how I have mine setup:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

[tableView deselectRowAtIndexPath:indexPath animated:YES];

SecondLevelController *anotherViewController = [[SecondLevelController alloc] initWithStyle:UITableViewStylePlain];

anotherViewController.myTitle = [tableView cellForRowAtIndexPath:indexPath].text;

[self.navigationController pushViewController:anotherViewController animated:YES];

[anotherViewController release];


What is being done here is the same row is being deselected as before.  This doesn't cancel the selection, or anything, all it does is drops the blue color on the background of the cell.  

Then it creates the next view controller, and sets its myTitle property.  The reason I did this was to show an example of how we can use indexPath and a property to make sure the next level of our drill down has the unique attribute it needs.  Then we push the view controller onto the stack.  When you run this you'll see it's how we want it.  The title is set with whatever you select in the list.

Here's the source code of up until this part: Download Source Code for UITableView Part III

While I won't go too far into depth I can explain briefly another practical use of this.  One method I have used is to keep my entire drill-down navigation in an NSArray.

If I were to have an organization of something such as the NFL we might have something like:




What I would do is have the NSArray hold the divisions which would be stored as an NSDictionary each.

The NSDictionary would have fields such as "Name" (String), "Teams" (Array of type NSDictionary), and "Conference" (String)

The "Teams" are the next level of navigation, which would have the fields such as "Name" (String) and "Players" (Array of type NSDictionary)

This may be difficult to understand with only words, but through the use of the ideas from above you could definitely create a drill-down of your own with your needed implementation.

1 comment:

  1. Hey, just wanted to thank you for this tutorial. Still applicable even with the 4.3 SDK, even though a few items in your code needed a little updating.