Table View Tutorial – Part 3
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 This tutorial is depricatedWelcome to the third part of UITableViewTutorial where I show you how to pass data from view to another.
- 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 this tutorial, we will look at how to pass data from one view to another. If you haven’t read the Part 2 of this tutorial, I suggest you do that here.
In this tutorial, we will learn how to
Passing data from view to another.
To pass data from one view to another, we need a receiving container at the other end of the view. In this example, we will pass the current row number selected in the table view. We will show this number in the detail view. To accomplish this we will declare a variable called rowNumber of type NSInteger in SubViewController (created in Part 2 of this tutorial). When a view is loaded viewDidLoad method is called on the view. It is in this method that we will set the text of the label. This is how the header file and the implementation file of the SubViewController is going to look like
@interface SubViewController : UIViewController {
IBOutlet UILabel *lblMessage;
NSInteger rowNumber;
}
@property (nonatomic, readwrite) NSInteger rowNumber;
@property (nonatomic, retain) UILabel *lblMessage;
@end
SubViewController.m
- (void)viewDidLoad {
lblMessage.text = [NSString stringWithFormat:@"Selected row number: %i", rowNumber];
}
We have also declared an object of type UILabel called lblMessage which will be connected to the label on the view. Notice that it is marked with IBOutlet keyword, telling the Interface Builder to make it available, so a connection can be created with the label on the view. Don’t forget to synthesize and release the label’s memory in the dealloc method.
This how the information is passed in didSelectRowAtIndexPath
-(void)tableView:(UITableView *)tblView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//Initialize the controller.
if(svController == nil)
self.svController = [[SubViewController alloc] initWithNibName:@”SubView” bundle:[NSBundle mainBundle]];
//Pass the current row number to the sub view controller.
svController.rowNumber = indexPath.row;
//Add the view as a sub view to the urrent view.
[self.view addSubview:[svController view]];
}
It is always hard to talk about something visual, so here is a little video of what goes on in the Interface Builder
If you have to pass an object, declare an object of the type you would like to pass.
Loading a view on button click
Now that we are in the SubView, we need some way to go back to the TableView. To do this, we are going to declare an event in the SubViewController which takes an id and returns IBAction (fancy word for void). However, IBAction does something else, it makes the method available in the Interface Builder, so a Button can register to the event.
This is how the header file and the implementation file of the SubViewController is going to look like
#import <UIKit/UIKit.h>
@class TableViewController;
@interface SubViewController : UIViewController {
IBOutlet UILabel *lblMessage;
NSInteger rowNumber;
TableViewController *tvController;
}
@property (nonatomic, readwrite) NSInteger rowNumber;
@property (nonatomic, retain) UILabel *lblMessage;
@property (nonatomic, retain) TableViewController *tvController;
-(IBAction)buttonClicked:(id)sender;
@end
SubViewController.m
-(IBAction)buttonClicked:(id)sender {
if(self.tvController == nil)
tvController = [[TableViewController alloc] initWithNibName:@”TableView” bundle:[NSBundle mainBundle]];
[self.view addSubview:[tvController view]];
}
All we are doing is declaring a method and implementing it in the implementation file of the SubViewController. How the view “TableView” is getting added is the same as how the “SubView” is getting added.
Here is another quick Interface Builder video.
Important
Since, this tutorial does not use a navigation controller some things will be different while passing data. In didRowSelectAtIndexPath, we check to see if the subViewController is nil or not, however is our case it will always be nil even when the view “TableView” is added from the “SubView”. If we were using a navigation controller, viewDidLoad only gets called once, any subsequent requests to “push” the view at the top of the stack of the navigation controller does not result in calling of the viewDidLoad method. In such situations, we either have to give our own implementation of the get/set methods of the property and it is there that we set the label’s text, also in the viewDidLoad method, for that one time it will be called.
There will be a tutorial on how to pass data using a Navigation Controller with a UITablevView.
It is much easier to have shorter tutorials, then the ones I had earlier. I can now talk about couple of functions at a time, instead of giving a complete solution which took lots of time to write. I hope you enjoy them and it is helpful to you.
You can download the source code for this tutorial here.
Please leave me your comments and if you have any questions email me at iphonearticles {@] gmail [.} com.
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 8 Responses to Table View Tutorial – Part 3
Tags





>@MoonDark you can use popToRootViewController:Animated or popToViewController:Animated
Let me know if this works for you
Happy Programming,
iPhone SDK Articles
>Hi , I have a question: How can I make like a “Back Button” or something, to be able to return to the main menu ?
Thanks for the tutorial
>Thank you for your tutorial, short is the best for the beginning
Hope you will continue like this !
thanks again
>Yes, this tutorial creates multiple objects in memory as this tutorial is missing the UINavigationController object. With the help of a navigation controller we would have done things a little differently.
Happy Programming,
iPhone SDK Articles
>Great job and many thanks.
Joris
>Hello,
I am not sure this code creates multiple TableViews or not. But, it seems to me, memory allocation is increasing at every ‘addSubview’.
So, I use [self.view removeFromSuperview]; instead of addSubview in buttonClicked.
How do you think?
>Hello,
Thank you for your message. There will not be 4000 rows in memory, because the UITableViewCell is reused based on an identifier.
This tutorial only show how to pass data among two views using a UITableView. So it may seem that, we will have multiple versions of the same table view in memory. This will not be a problem, if there was a navigation controller on the view. Since it would have given us a way to go back, as I do not have one I had to create a button to go back.
Thanks,
iPhone SDK Articles
>Just a quick question: Will there now be created multiple TableViews? Lets say the table view holds 1000 rows. If the user navigates back and forth 4 times, will there now be 4000 rows in memory?
Great articles, by the way. It really helps a C++/C# guy to get it back to basics.