How to Use uitableview (1)-create a simple list

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:

#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

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.