Welcome to Scribd, the world's digital library. Read, publish, and share books and documents. See more
Standard view
Full view
of .
Look up keyword
Like this
0 of .
Results for:
No results containing your search query
P. 1
_2_Loading a Detail View

_2_Loading a Detail View

Ratings: (0)|Views: 0|Likes:
Published by Chuột Fx
_2_Loading a Detail View
_2_Loading a Detail View

More info:

Categories:Types, School Work
Published by: Chuột Fx on Aug 13, 2013
Copyright:Attribution Non-commercial


Read on Scribd mobile: iPhone, iPad and Android.
download as DOC, PDF, TXT or read online from Scribd
See more
See less





UITableView – Loading a detail view
In this tutorial, you will learn how to navigate to the detail view and also pass some data at thesame time. This is the second tutorial in the UITableView tutorial series and inherits its sourcecode from the first tutorial.
Creating a detail view
Open Interface Builder and click on File -> New -> (select Cocoa Touch) View, save it in theapplication directory and name it “DetailView”. You will be asked to add the view to the current project, click on “Add”. You may need to drag the view (in XCode) to the “Resources” folder. Now that you have your view, we will create a view controller class to control the view on thescreen. In XCode select Classes then click on File -> New File -> (under iPhone OS) selectUIViewController subclass and name it “DetailViewController, do not change the extension. Now we have to connect the view to the view controller we just created. In Interface Builder,select File’s Owner and open Identity Inspector, under class Identity set the class to“DetailViewController”, open Connections Inspector and create a connection from the view property to the view object in the nib file. Now add controls to the view which will display the detail contents. The controls that you maywant to add to the view, depends on the data you want to display. We will display the countryselected in the table view, so a simple label should do. Drag and drop the label on the view. Weneed some way to change the text of the label from XCode, create a variable of typeUILabelinxcode and connect it with the label object on the view. The label should be declared withIBOutlet property, so it shows up in the Connections Inspector. This is how the code looks like
#import <UIKit/UIKit.h> @interfaceDetailViewController:UIViewController{  IBOutlet UILabel*lblText;} @end 
//Dealloc method declared in DetailViewController.m
- (void)dealloc{  [lblText release]; [super dealloc]; }
After you have declared the variable, open IB and connect the variable to the label placed on theview in Connections Inspector. Now we can change the label’s properties from XCode.
Navigate to the detail view
The methodtableView:didSelectRowAtIndexPath is called when a row is selected, it passes the tableview object with the indexPath object to tell us which row was selected. First import the“DetailViewController” class in RootViewController, so it knows about it. The following codewill initialize the detail view and display it
- (void)tableView:(UITableView*)tableView didSelectRowAtIndexPath: (NSIndexPath *)indexPath{  DetailViewController*dvController= [[DetailViewController alloc]  initWithNibName:@"DetailView"bundle:[NSBundlemainBundle]]; [self.navigationController pushViewController:dvController animated:YES]; [dvController release]; dvController= nil; }
A “DetailViewController” is created, initialized withinitWithNibName:bundlemessage and thename of the nib file is passed as the parameter. The view controller is then push to the top of thestack with its animated property set to YES. At last, we clean up memory by releasing the detailview controller. Run the application and now you can select a row to see the detail view.
Passing data
We still have to pass the selected country from the list to the detail view. To do this, we willdeclare a property in “DetailViewController” whose data type is the same as the in the array, inour case NSString. This is what you have to do if you want to pass data from one view controller to another. The following code declares a property in “DetailViewController”
#import <UIKit/UIKit.h> @interfaceDetailViewController:UIViewController{  IBOutlet UILabel*lblText;NSString *selectedCountry; } @property (nonatomic, retain) NSString *selectedCountry;  @end 
//Dealloc method declared in DetailViewController.m
- (void)dealloc{  [selectedCountry release]; [lblText release]; [super dealloc]; } 
//First three lines of DetailViewController.m
#import "DetailViewController.h" 
@implementationDetailViewController @synthesizeselectedCountry;
The property is synthesized at the top after the implementation begins. Now we can pass theselected country from the table view to the detail view. ThetableView:didSelectRowAtIndexPath method looks like this
- (void)tableView:(UITableView*)tableView didSelectRowAtIndexPath: (NSIndexPath *)indexPath{  
//Get the selected country 
NSString *selectedCountry= [listOfItems objectAtIndex:indexPath.row];  
//Initialize the detail view controller and display it.
DetailViewController*dvController= [[DetailViewController alloc]  initWithNibName:@"DetailView"bundle:[NSBundlemainBundle]]; dvController.selectedCountry=selectedCountry;[self.navigationController pushViewController:dvController animated:YES]; [dvController release]; dvController= nil; }
We first get the selected country from the array, initialize the detail view controller, set theselected country to the property on the detail view controller and display it.
Setting the accessory view
Run the app and now we are able to select a row in a table view. However, it is not obvious tothe user that a row can be selected to see its detail view. We can add a “accessory view” to thecell which will show up at the right end of the row. The accessory view can be set up intableView:cellForRowAtIndexPathmethod or intableView:accessoryTypeForRowWithIndexPath. We will use the later method to keep our codesimple. This is how the source code changes
- (UITableViewCellAccessoryType)tableView:(UITableView*)tableView accessoryTypeForRowWithIndexPath:(NSIndexPath *)indexPath{  
//return UITableViewCellAccessoryDetailDisclosureButton;
The above method returns an enum UITableViewCellAccessoryType and we can return four  values: UITableViewCellAccessoryNone, UITableViewCellAccessoryDisclosureIndicator,UITableViewCellAccessoryDetailDisclosureButton, andUITableViewCellAccessoryCheckmark. You can test the code by returning one of the four values to see how the accessory view looks like. If you return“UITableViewCellAccessoryDetailDisclosureButton” clicking on the button will not doanything, since the cell is not selected but a button is clicked. The SDK does provide a methodwhich gets called when the accessory button is clicked and that is called

You're Reading a Free Preview

/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->