TableView深入研究

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-什么是tableView的内容(content)?

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

  • 2-contentSize.height :

意思是内容的高度.

  • 3-contentOffset.y :

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

  • 4-contentInset :

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

  • 5-Frame:

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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 解决添加到ScrollView上的UITableView控件自动向下偏移64像素的问题 首先理解1:即使UITab...
    CoderZb阅读 10,719评论 1 8
  • 1、禁止手机睡眠[UIApplication sharedApplication].idleTimerDisabl...
    DingGa阅读 4,814评论 1 6
  • 站多了, 脚就累了。 那就坐一会儿, 直到脚元气恢复……
    小剧在成长阅读 1,148评论 0 5
  • 我抬眼对白发老母说:“走了!”拿起行李,二话不说,出了家门,大步前行,头也不回。 离开了身后的视线,我慢下脚步,任...
    离群索居的野兽阅读 1,837评论 0 0
  • 在中国普通群众所能接触到的理财产品有: 1.股票、基金、债券、P2P 2.私募、专户理财、外汇、期货 3.黄金、收...
    镜花奇缘阅读 4,642评论 1 2

友情链接更多精彩内容