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