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:
#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:
// Returns the total number of 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 labels: Tag]; If (cell = nil) {Cell = [[uitableviewcell alloc] initwithframe: cgrectzero reuseidentifier: Tag] autorelease];} nsuinteger ROW = [indexpath row]; // sets the text cell. TEXT = [listdata objectatindex: Row]; // The selected color does not change. Set the following parameters: // cell. selectionstyle = uitableviewcellselectionstylenone; // you do not need to split the line. // tableview. separatorstyle = uitableviewcellseparatorstylenone; return cell ;}
4. respond to user click events to implement the uitableviewdelegate protocol, as shown below:
// Response user Click Event-(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 alloc]; [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:
# 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 viewd Idunload]; // release any retained subviews of the main view.}-(bool) shouldautorotatetointerfaceorientation :( uiinterfaceorientation) interfaceorientation {return (interfaceorientation! = Uiinterfaceorientationportraitupsidedown);} # pragma mark-Table View data source delegate // 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 labels: Tag]; If (cell = nil) {Cell = [[uitableviewcell alloc] initwithframe: cgrectzero reuseidentifier: Tag] autorelease];} nsuinteger ROW = [indexpath row]; // sets the text cell. TEXT = [listdata objectatindex: Row]; // The selected color does not change. Set the following parameters: // cell. selectionstyle = uitableviewcellselectionstylenone; // you do not need to split the line. // tableview. separatorstyle = uitableviewcellseparatorstylenone; return cell;} # pragma mark-Table View data delegate // response to user Click Event-(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 alloc]; [showselection show];} @ end
Download the instance source code:
Http://download.csdn.net/detail/ztp800201/4500391
Compiling environment: Mac OS 10.7.4 + xcode4.3.3 + ios5.1