Simple UITableView application developed by iPhone (5)

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.