[复习]
tableView展示数据”三部曲”
1>.遵守协议”数据源协议”
2>.设置数据源
3>.实现数据源方法
UITableViewController体验
- 相当于一个控制器自带tableView
- viewController管理的是view
- tableViewController管理的是tableView 是全屏的
- 在控件UITableViewController下self.view = self.tableView
//1>.控件建立后便已自动遵守数据协议
//2>.并已设置数据源
//3>.修改ViewController.h 继承 UITableViewController
//4>.修改Main.storyboard中View Controller的class -> ViewController
注意:一定要记得勾选 is Initial View Controller —>
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
//5>.数据源方法
//组数 = 1
//行数 = 2
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 2;
}
//cell内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"A" forIndexPath:indexPath];
//cell背景色
cell.backgroundColor = [UIColor colorWithRed:(arc4random_uniform(256)/ 255.0) green:(arc4random_uniform(256)/ 255.0) blue:(arc4random_uniform(256)/ 255.0) alpha:1.0];
return cell;
}
@end
tableView的HeaderView和FooterView
- tableView的header - (x, y, width) 可以随便指定,(height) 实际数值
- tableView的footer - (y, width) 可以随便指定,(x, height) 实际数值
- (void)viewDidLoad {
[super viewDidLoad];
UIView *headerView = [[UIView alloc] init];
//尺寸
headerView.frame = CGRectMake(0, 0, 0, 150);
//颜色
headerView.backgroundColor = [UIColor blueColor];
//头部视图
self.tableView.tableHeaderView = headerView;
UIButton *btn = [[UIButton alloc] init];
//尺寸
btn.frame = CGRectMake(0, 0, 0, 44);
//颜色
btn.backgroundColor = [UIColor blackColor];
//尾部视图
self.tableView.tableFooterView = btn;
}