IPhone UITableView usage (1)

Source: Internet
Author: User

UITableView is commonly used in iPhone and has many controls. Here we use UITableView to create a simple table. The effect is as follows:

 


To add data to the table, you need to add the UITableViewDataSource protocol.

To respond to user clicks, you need to add the UITableViewDelegate protocol.

 


1. Create a project: use the template Single View Application to create a project. Only iPhone is supported.

2. Add the UITableViewDataSource and UITableViewDelegate protocols in ViewController. h, as shown below:


[Cpp]
# Import <UIKit/UIKit. h>
 
@ Interface ViewController: UIViewController <UITableViewDataSource, UITableViewDelegate> {
 
NSArray * listData;
}
 
@ Property (nonatomic, retain) NSArray * listData;
 
@ End

# Import <UIKit/UIKit. h>

@ Interface ViewController: UIViewController <UITableViewDataSource, UITableViewDelegate> {

NSArray * listData;
}

@ Property (nonatomic, retain) NSArray * listData;

@ End

3. Add data to the list to implement the UITableViewDataSource protocol, as shown below:


[Cpp] view plaincopyprint? // The total number of returned rows
-(NSInteger) tableView :( UITableView *) tableView
 
NumberOfRowsInSection :( NSInteger) section
 
{
Return [self. listData count];

}
 
// Add information for each row
-(UITableViewCell *) tableView :( UITableView *) tableView
CellForRowAtIndexPath :( NSIndexPath *) indexPath
 
{

NSString * tag = @ "tag ";

UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier: tag];

If (cell = nil ){
Cell = [[[UITableViewCell alloc] initWithFrame: CGRectZero
ReuseIdentifier: tag] autorelease];
}

NSUInteger row = [indexPath row];

// Set text
Cell. text = [listData objectAtIndex: row];

// Make the following settings if the selected color does not change.
// Cell. selectionStyle = UITableViewCellSelectionStyleNone;

// No split line is required
// TableView. separatorStyle = UITableViewCellSeparatorStyleNone;

Return cell;

}

// The total number of returned rows
-(NSInteger) tableView :( UITableView *) tableView

NumberOfRowsInSection :( NSInteger) section

{
Return [self. listData count];

}

// Add information for each row
-(UITableViewCell *) tableView :( UITableView *) tableView
CellForRowAtIndexPath :( NSIndexPath *) indexPath

{

NSString * tag = @ "tag ";

UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier: tag];

If (cell = nil ){
Cell = [[[UITableViewCell alloc] initWithFrame: CGRectZero
ReuseIdentifier: tag] autorelease];
}

NSUInteger row = [indexPath row];

// Set text
Cell. text = [listData objectAtIndex: row];

// Make the following settings if the selected color does not change.
// Cell. selectionStyle = UITableViewCellSelectionStyleNone;

// No split line is required
// TableView. separatorStyle = UITableViewCellSeparatorStyleNone;

Return cell;

}

4. respond to user click events to implement the UITableViewDelegate protocol, as shown below:


[Cpp]
// Respond to user-clicked events
-(Void) tableView :( UITableView *) tableView didSelectRowAtIndexPath :( NSIndexPath *) indexPath {

UIAlertView * showSelection;
NSString * message;

Message = [[NSString alloc] initWithFormat: @ "You chose the: % @",
[Self. listData objectAtIndex: indexPath. row];

ShowSelection = [[UIAlertView alloc]
InitWithTitle: @ "Selected"
Message: message
Delegate: nil
CancelButtonTitle: @ "OK"
OtherButtonTitles: nil];

[ShowSelection autorelease];
[ShowSelection show];
}

// Respond to user-clicked events
-(Void) tableView :( UITableView *) tableView didSelectRowAtIndexPath :( NSIndexPath *) indexPath {

UIAlertView * showSelection;
NSString * message;

Message = [[NSString alloc] initWithFormat: @ "You chose the: % @",
[Self. listData objectAtIndex: indexPath. row];

ShowSelection = [[UIAlertView alloc]
InitWithTitle: @ "Selected"
Message: message
Delegate: nil
CancelButtonTitle: @ "OK"
OtherButtonTitles: nil];

[ShowSelection autorelease];
[ShowSelection show];
}


5. Add UITableView to ViewController and connect the delegate and dataSource of UITableView to ViewController. As shown in:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

6. complete code is as follows:


[Cpp]
# Import "ViewController. h"
 
@ Interface ViewController ()
 
@ End
 
@ Implementation ViewController
 
@ Synthesize listData;
 
-(Void) viewDidLoad
{

Self. listData = [[NSArray alloc] initWithObjects: @ "Item1", @ "Item2", @ "Item3", @ "Item4", @ "Item5", @ "Item6 ", @ "Item7", nil];
 
[Super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
 
-(Void) viewDidUnload
{
Self. listData = nil;

[Super viewDidUnload];
// Release any retained subviews of the main view.
}
 
-(BOOL) shouldAutorotateToInterfaceOrientation :( UIInterfaceOrientation) interfaceOrientation
{
Return (interfaceOrientation! = UIInterfaceOrientationPortraitUpsideDown );
}
 
 
# Pragma mark-Table view data source delegate
 
// The total number of returned rows
-(NSInteger) tableView :( UITableView *) tableView
NumberOfRowsInSection :( NSInteger) section
{
Return [self. listData count];
}
 
// Add information for each row
-(UITableViewCell *) tableView :( UITableView *) tableView
CellForRowAtIndexPath :( NSIndexPath *) indexPath
 
{

NSString * tag = @ "tag ";

UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier: tag];

If (cell = nil ){
Cell = [[[UITableViewCell alloc] initWithFrame: CGRectZero
ReuseIdentifier: tag] autorelease];
}

NSUInteger row = [indexPath row];

// Set text
Cell. text = [listData objectAtIndex: row];

// Make the following settings if the selected color does not change.
// Cell. selectionStyle = UITableViewCellSelectionStyleNone;

// No split line is required
// TableView. separatorStyle = UITableViewCellSeparatorStyleNone;

Return cell;

}
 
# Pragma mark-Table view data delegate
 
// Respond to user-clicked events
-(Void) tableView :( UITableView *) tableView didSelectRowAtIndexPath :( NSIndexPath *) indexPath {

UIAlertView * showSelection;
NSString * message;

Message = [[NSString alloc] initWithFormat: @ "You chose the: % @",
[Self. listData objectAtIndex: indexPath. row];

ShowSelection = [[UIAlertView alloc]
InitWithTitle: @ "Selected"
Message: message
Delegate: nil
CancelButtonTitle: @ "OK"
OtherButtonTitles: nil];

[ShowSelection autorelease];
[ShowSelection show];
}
Author: ztp800201
 

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.