TableView 的使用 实例一
TableView 是iphone/ipad中常常会用到的导航控件,本实例我们开始做一个基本的导航菜单列表,通过该例可以让大家了解该控件的基础知识及基本使用的方法,废话少说开始。
一、首先我们先创建一个iphone或ipad工程(本例以iphone为例)命名TableViewDemo1
如下图所示:
二、打开TableViewDemo1ViewController.xib,添加TableView控件。
三、编辑TableViewDemo1ViewController.h
添加实现的协议UITableViewDelegate,UITableViewDataSource,及声明UITableView对象tableViewList
@interface TableViewDemo1ViewController : UIViewController<UITableViewDelegate,UITableViewDataSource> {IBOutlet UITableView *tableViewList;}@end



@interface TableViewDemo1ViewController : UIViewController<UITableViewDelegate,UITableViewDataSource> {IBOutlet UITableView *tableViewList;NSMutableArray *dataItems;}@end- (void)viewDidLoad { [super viewDidLoad];dataItems=[[NSMutableArray alloc]initWithObjects:@"中国",@"美国",@"日本",nil];}- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ return 1;}- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [dataItems count]; }// Customize the appearance of table view cells.- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease]; }NSUInteger row=[indexPath row];cell.textLabel.text=[dataItems objectAtIndex:row]; return cell;}