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;
}


*添加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;

*那么思考普通的控件会纳入contentSize计算范围吗?
// 额外添加的子控件
UIView *textHeader = [[UIView alloc] init];
textHeader.frame = CGRectMake(0, -40, tableView.frame.size.width, 40);
textHeader.backgroundColor = [UIColor purpleColor];
[tableView addSubview:textHeader];

*要如何显示额外添加的控件?
// 内边距
tableView.contentInset = UIEdgeInsetsMake(64, 0, 49, 0);

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










总结:
-
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 : 以父控件内容的左上角为坐标原点
网友评论