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