First, drag the table View to the View window and drag the circle next to the data source and delegate connection to the file's owner icon. In this way, the control class becomes the data source and delegate of the table.
The header file code of the View Controller is as follows:
# Import <uikit/uikit. h> // Let the class followThe uitableviewdelegate and uitableviewdatasource protocols act as the delegate and Data Source of the table view.@ Interface szlviewcontroller: uiviewcontroller<Uitableviewdelegate, uitableviewdatasource>{
// Declare an array for storing the data to be displayedNsarray*Listdata;}@ Property (nonatomic, retain) nsarray* Listdata;@ End
Implementation Code:
#import "SZLViewController.h"@implementation SZLViewController@synthesize listData;- (void)viewDidLoad{ [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. NSArray *array = [[NSArray alloc] initWithObjects:@"GuangZhou", @"ShenZhen", @"BeiJing", @"ShangHai", @"TianJing", @"HangZhou", @"NingBo", @"HongKong", @"ZhuHai", @"XiZang", @"XinJiang", @"ChangSha", @"NanNing", @"WuLuMuQi", nil]; self.listData = array; [array release];}- (void)viewDidUnload{ [super viewDidUnload]; // Release any retained subviews of the main view. // e.g. self.myOutlet = nil; self.listData = nil;}-(void)dealloc{ [listData release]; [super release];}
Add the following code:
/*** Check the number of rows in the specified partition. The default number of partitions is 1. This method is used to return the number of rows in the table partitions that constitute the list */-(nsinteger) tableview :( uitableview *) tableview numberofrowsinsection :( nsinteger) Section {return [self. listdata count];}/*** this method is called when a table is to draw a row */-(uitableviewcell *) tableview :( uitableview *) tableview cellforrowatindexpath :( nsindexpath *) indexpath {// static string instance, acting as the key to represent a form element static nsstring * simpletableidentifier = @ "simpletableidentifier"; // simpletableidentifier type reusable unit, uitableviewcell * cell = [tableview identifier: simpletableidentifier]; If (cell = nil) {Cell = [[[uitableviewcell alloc] initwithstyle: Invalid reuseidentifier: simpletableidentifier] autorelease];} // obtain the rows to be displayed in the table view from the indexpath variable nsuinteger ROW = [indexpath row]; // obtain the corresponding string cell from the array. textlabel. TEXT = [listdata objectatindex: Row]; return cell ;}