Table View Tutorial – Part 2
IMPORTANT:
The following tutorial has been deprecated and should no longer be used for any purposes. To learn more on how to use the UITableView click here Welcome to the second part of UITableView tutorial where I show you how to select a UITableViewCell
- Part 1 – Creating a simple Table View
- Part 2 – Selecting a UITableViewCell.
- Part 3 – Passing data to a detail view.
- Part 4 – UITableViewCell with multiple columns.
In the second part of the tutorial, we will be able too
If you haven’t read the first part of the tutorial, I suggest you read it here before reading this one. Download the source code from part 1, as we will use it as a base to start off from.
Add an accessory type for each row
Basically, showing visually to the user that a row has a detail view, or extended properties. To do this, we have to implement accessoryTypeForRowWithIndexPath method which returns an UITableViewCellAccessoryType object and takes an IndexPath object. We will return UITableViewCellAccessoryDetailDisclosureButton to indicate that there exists a detail view to the present row.
- (UITableViewCellAccessoryType)tableView:(UITableView *)tv accessoryTypeForRowWithIndexPath:(NSIndexPath *)indexPath {
return UITableViewCellAccessoryDetailDisclosureButton;
}
Handle events when a row is selected
didSelectRowAtIndexPath is called whenever a row is selected. Before we write any code for this method, we first need a view and its view controller object. Create a new view in Interface Builder and name it SubView. Interface Builder will ask you to add the view to your project. After you do that, move the view under the resources folder. Click on File -> New File -> UIViewController subclass in Xcode to create a new view controller. Name your view controller class “SubViewController”.
Important
Whenever you create a new view and a view controller
- we have too set the view controller class as the class that the File’s Owner proxy object will use in Interface Builder. Do this by selecting File’s Owner -> Click Tools -> Identity Inspector -> Select SubViewController class from the class drop down.
- Click on Connections Inspector -> connect the view variable to the view in the nib file.
Next thing we have to do is tell the TableViewController class about the SubViewController. We do this by adding a forward declaration to SubViewController. We then create a object of type SubViewController and a property. This is how the header file will look like.
@class SubViewController;
@interface TableViewController : UIViewController {
IBOutlet UITableView *tblView;
SubViewController *svController;
}
@property (nonatomic, retain) UITableView *tblView;
@property (nonatomic, retain) SubViewController *svController;
@end
Don’t forget to synthesize and release the SubViewController’s memory in the dealloc method. Since we only added a forward declaration in the header file, we need to import the “SubViewController” class in the implementation file of the TableViewController.
This is how we would synthesize and release the SubViewController’s memory
synthesize svController;
//Above line goes after the implementation <ClassName> line.
-(void)dealloc {
[svController release];
}
Now that we have created the sub view, sub view controller and told the TableViewController about the SubViewController. We can handle the didSelectRowAtIndexPath method.
-(void)tableView:(UITableView *)tblView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//Initialize the controller.
if(svController == nil)
self.svController = [[SubViewController alloc] initWithNibName:@”SubView” bundle:[NSBundle mainBundle]];
//Add the view as a sub view to the urrent view.
[self.view addSubview:[svController view]];
}
Load a nib file
Here we see that the view is added as a sub view to the current view and not pushed in the navigation controller, as we do not have one. This is how we load a nib file. If you notice the code is almost the same as the code in applicationDidFinishLoading method. didSelectRowAtIndexPath is found in UITableViewDelegate class, since TableViewController is marked as a delegate for the table view, this class receives any events raised on the table view.
You can download the source code here. In my next tutorial, I will show how to pass data from one view to another view.
Please let me know your thoughts and email me at iphonearticles [@] gmail {.} com if you have any questions or post a question here.
5 Responses to Table View Tutorial – Part 2
Tags





Hi!
I am new to IOS sdk. Can you help me with my sample project?
What I am trying to do is :
In a login view, If the user name/password matches with what I enter in text fields, it should take me to another view in which I enter a code, it should check with my list and in another view I want to display the data for that corresponding code
Can you help me with this?
thanks
>@Hal if you send me your source code, I will be happy to take a look.
Happy Programming,
iPhone SDK Articles
>Hey, nice tutorial.
However, something strange happened in my project.
I’m new to the iPhone SDK, so please bear with me: whenever i click in a row, it gets selected but the subview won’t load… but if i click on it again, it will load.
Any ideas why? :/
Thanks in advance!
>I think you forgot to mention to connect the file’s owner to the table delegate.
or at least I missed it in the tutorial.
keep up the good work!
>This was great. Really glad you’re doing this. I can’t wait for the navigation controllers, tabbars etc etc…