1. click File> New> Proget... to create a project:
2. Select the Single View Application template and click Next:
3. Name the project Simple Table and click Next-> Create to Create a new project:
4. Click the ViewController. xib file and add the control Table View to the View:
5. add data and method Delegation to the control Table View:
6. Click the ViewController. h header file to add data members to ViewController:
[Plain]
# Import <UIKit/UIKit. h>
@ Interface ViewController: UIViewController <UITableViewDelegate, UITableViewDataSource>
{
NSArray * list;
}
@ Property (nonatomic, retain) NSArray * list;
@ End
7. Click the ViewController. m file to add data and Protocol methods to ViewController:
[Plain]
# Import "ViewController. h"
@ Interface ViewController ()
@ End
@ Implementation ViewController
@ Synthesize list;
-(Void) viewDidLoad
{
[Super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSArray * array = [[NSArray alloc] initWithObjects: @ "Guangdong", @ "Hunan", @ "Beijing", @ "Shanghai", @ "Hong Kong ", @ "Macao", nil];
Self. list = array;
[Array release];
}
-(Void) viewDidUnload
{
[Super viewDidUnload];
// Release any retained subviews of the main view.
Self. list = nil;
}
-(BOOL) shouldAutorotateToInterfaceOrientation :( UIInterfaceOrientation) interfaceOrientation
{
Return (interfaceOrientation! = UIInterfaceOrientationPortraitUpsideDown );
}
-(Void) dealloc
{
[Super dealloc];
[List release];
}
# Pragma mark-
# Pragma mark Table Data Source Methods
-(NSInteger) tableView :( UITableView *) tableView numberOfRowsInSection :( NSInteger) section
{
Return [list count];
}
-(UITableViewCell *) tableView :( UITableView *) tableView cellForRowAtIndexPath :( NSIndexPath *) indexPath
{
Static NSString * identfier = @ "placetable ";
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier: identfier];
If (cell = nil)
{
Cell = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier: identfier];
}
NSInteger row = [indexPath row];
Cell. textLabel. text = [list objectAtIndex: row];
Return cell;
}
-(Void) tableView :( UITableView *) tableView didSelectRowAtIndexPath :( NSIndexPath *) indexPath
{
NSInteger row = [indexPath row];
NSString * message = [list objectAtIndex: row];
UIAlertView * alert = [[UIAlertView alloc] initWithTitle: @ "you choose:" message: message delegate: self cancelButtonTitle: @ "OK" otherButtonTitles: nil];
[Alert show];
[Alert release];
}
@ End
8. Now you can click the run button to test the program and click the option "Guangdong". The effect is as follows:
6. Click the ViewController. h header file to add data members to ViewController:
[Plain]
# Import <UIKit/UIKit. h>
@ Interface ViewController: UIViewController <UITableViewDelegate, UITableViewDataSource>
{
NSArray * list;
}
@ Property (nonatomic, retain) NSArray * list;
@ End
7. Click the ViewController. m file to add data and Protocol methods to ViewController:
[Plain]
# Import "ViewController. h"
@ Interface ViewController ()
@ End
@ Implementation ViewController
@ Synthesize list;
-(Void) viewDidLoad
{
[Super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSArray * array = [[NSArray alloc] initWithObjects: @ "Guangdong", @ "Hunan", @ "Beijing", @ "Shanghai", @ "Hong Kong ", @ "Macao", nil];
Self. list = array;
[Array release];
}
-(Void) viewDidUnload
{
[Super viewDidUnload];
// Release any retained subviews of the main view.
Self. list = nil;
}
-(BOOL) shouldAutorotateToInterfaceOrientation :( UIInterfaceOrientation) interfaceOrientation
{
Return (interfaceOrientation! = UIInterfaceOrientationPortraitUpsideDown );
}
-(Void) dealloc
{
[Super dealloc];
[List release];
}
# Pragma mark-
# Pragma mark Table Data Source Methods
-(NSInteger) tableView :( UITableView *) tableView numberOfRowsInSection :( NSInteger) section
{
Return [list count];
}
-(UITableViewCell *) tableView :( UITableView *) tableView cellForRowAtIndexPath :( NSIndexPath *) indexPath
{
Static NSString * identfier = @ "placetable ";
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier: identfier];
If (cell = nil)
{
Cell = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier: identfier];
}
NSInteger row = [indexPath row];
Cell. textLabel. text = [list objectAtIndex: row];
Return cell;
}
-(Void) tableView :( UITableView *) tableView didSelectRowAtIndexPath :( NSIndexPath *) indexPath
{
NSInteger row = [indexPath row];
NSString * message = [list objectAtIndex: row];
UIAlertView * alert = [[UIAlertView alloc] initWithTitle: @ "you choose:" message: message delegate: self cancelButtonTitle: @ "OK" otherButtonTitles: nil];
[Alert show];
[Alert release];
}
@ End
8. Now you can click the run button to test the program and click the option "Guangdong". The effect is as follows:
Author: js_dada