#import <UIKit/UIKit.h>
@interface ViewController2 : UIViewController<UITableViewDataSource,UITableViewDelegate>
@end
在m文件裏面實現接口的協議
#import "ViewController2.h"
@interface ViewController2 ()
@end
@implementation ViewController2
//設置表格行數
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{return 5;}
// Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:
// Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)
//表格加載相關設置
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *identifier =@"cellIndentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if(cell==nil){
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
cell.textLabel.text =@"Cell title Here";
cell.detailTextLabel.text=@"Detail Information here";
}
return cell;
}
//設置高度
-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 80;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
//定義一塊區域顯示錶格
CGRect re = CGRectMake(0, 40, 300, 420);
// 設置背景顏色
self.view.backgroundColor=[UIColor whiteColor];
// 將表格加到區域內
UITableView *tableView = [[UITableView alloc] initWithFrame:re];
// 設置代理
tableView.delegate=self;
// 設置數據源代理
tableView.dataSource =self;
// 添加到頁面根視圖
[self.view addSubview:tableView];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end