07-控件UITableViewController的使用:-header & -footer

[复习]
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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章