NSString *myTitle;
And:
@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:
-Divisions
--Teams
---Players
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.
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.
ReplyDelete