1.在视图上创建TableView( 拖控件),为ViewController创建UITableView属性(链接至TableView)和NSArray属性(存储数据)
ViewController.h
@property (strong, nonatomic) NSArray *list;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
2.为UIViewController实现UITableViewDelegate,UITableViewDataSource两个协议
ViewController.h
@interface ViewController : UIViewController<UITableViewDelegate,UITableViewDataSource>
@property (strong, nonatomic) NSArray *list;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@end
3.同步属性
ViewController.c
@synthesize list = _list;
4.设置tableview数据源的数据,显示代理 以及显示区域等.
ViewController.c
- (void)viewDidLoad
{
[super viewDidLoad]; NSArray *array = [[NSArray alloc] initWithObjects:@"美国", @"菲律宾",
@"黄岩岛", @"中国", @"泰国", @"越南", @"老挝", @"日本" ,nil];
self.list = array;
[self.tableView setFrame:CGRectMake(0, 0, 320, 420)];
[self.tableView setDataSource:self];
[self.tableView setDelegate:self];}
5.绘制表格单元
ViewController.c
//Draw tableview cell
-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *TableSampleIdentifier = @"TableSampleIdentifier"; UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:TableSampleIdentifier]; if(cell == nil){
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:TableSampleIdentifier];
} NSUInteger row = [indexPath row]; cell.textLabel.text=[self.list objectAtIndex:row]; return cell;
}-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [self.list count];
}