iOS

TableView深入研究

2016-07-22  本文已影响699人  提莫不胖

OC 日常笔记碎片知识

1.分析TableView的重要性.
-TableView是如何计算contentSize?
-什么控件会纳入contenSize?

*代码演示

#import "ViewController.h"

@interface ViewController () <UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, weak) UITableView *tableView;
@end

@implementation ViewController

- (void)viewDidLoad 
{
    [super viewDidLoad];
    //创建tableView
    UITableView *tableView = [[UITableView alloc] init];
    //设置尺寸
    tableView.frame = CGRectMake(50, 100, 200, 300);
    //背景颜色
    tableView.backgroundColor = [UIColor grayColor];
    //数源方法
    tableView.dataSource = self;
    //代理方法
    tableView.delegate = self;
    //每行高度
    tableView.rowHeight = 40;
    //赋值view
    [self.view addSubview:tableView];
    self.tableView = tableView;
}

//点击屏幕打印tableView的contentSize
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    NSLog(@"contentSize.height = %f", self.tableView.contentSize.height);
}

#pragma mark - <UITableViewDelegate>
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"contentOffset.y = %f", tableView.contentOffset.y);
    NSLog(@"contentSize.height = %f", tableView.contentSize.height);
}

#pragma mark - 数据源
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 20;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *ID = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
        cell.backgroundColor = [UIColor redColor];
    }
    cell.textLabel.text = [NSString stringWithFormat:@"%@-%zd", self.class, indexPath.row];
    return cell;
}
根据数据多少计算contentSize.gif 注意TableView打印.gif

*添加TableHeaderView与TableFooterView

 //tableHeaderView
    UIView *header = [[UIView alloc] init];
    header.frame = CGRectMake(0, 0, tableView.frame.size.width, 64);
    header.backgroundColor = [UIColor yellowColor];
    tableView.tableHeaderView = header;
    //tableFooterView
    UIView *footer = [[UIView alloc] init];
    footer.frame = CGRectMake(0, 0, tableView.frame.size.width, 49);
    footer.backgroundColor = [UIColor blueColor];
    tableView.tableFooterView = footer;
增加头部与尾部View.gif

*那么思考普通的控件会纳入contentSize计算范围吗?

    // 额外添加的子控件
    UIView *textHeader = [[UIView alloc] init];
    textHeader.frame = CGRectMake(0, -40, tableView.frame.size.width, 40);
    textHeader.backgroundColor = [UIColor purpleColor];
    [tableView addSubview:textHeader];
并没有纳入计算范围.gif

*要如何显示额外添加的控件?

// 内边距
    tableView.contentInset = UIEdgeInsetsMake(64, 0, 49, 0);
增加额外滚动区域.gif

*添加额外的滚动区域并没有改变contentSize,唯一改变是可视范围.

01.png 02.png 03.png 04.png 05.png 06.png 07.png 08.png 09.png 10.png

总结:

1.Cell
2.tableHeaderView\tableFooterView
3.sectionHeader\sectionFooter

意思是内容的高度.

内容的偏移量(frame的顶部 - content的顶部)

内容周围的间距(内边距)

1.frame.size.height : 矩形框的高度
2.frame : 以父控件内容的左上角为坐标原点

上一篇 下一篇

猜你喜欢

热点阅读