[複習]
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;
}