什么是UITableView
1.它是继承自UIScrollView的,所以它支持滚动,但是只支持垂直滚动
2.它有两种样式,就是分组(UITableViewStylePlain)不分组(UITableViewStyleGrouped)
3.通过设置UITableView的数据源(DataSource)来显示数据
在代码中或在视图上设置好数据源后,就要实现数据源的方法
#pragma -mark tableView数据源
必须要实现的有两个方法
//这个方法返回的是这个tableView的各个组里有多少行数据,组数的获取是传入的section
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
//这个方法返回的是这个tableView的每个cell需要具体显示什么数据,这个方法会在每次创建cell时调用
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
可选实现的有下面的这些方法
//返回的这个tableView分了多少组,如果不分组的话,这个方法没有什么意义
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;
//设置组的头标题
- (nullable NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;
//设置组的尾标题
- (nullable NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section;
#pragma -mark cell的使用
cell中可以设置3种内容来显示数据
textLabel,detailLabel,imageView
cell的四种style
UITableViewCellStyleDefault,UITableViewCellStyleValue1,UITableViewCellStyleValue2,UITableViewCellStyleSubtitle
-----如何去设置cell的高度
1.通过self.tableView.rowHeight 可以为所有cell设置高度,但是只能设置相同的高度
2.若想为tableView的cell设置不同的高度,就要实现tableView的代理啦
#pragma mark -tableView的代理
tableView的代理都是可选实现的,
//这个就是为不同的cell设置不同的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
-------cell的重用
当cell出屏幕的时候,把cell对象存储在特定标示的缓存池中,当tableView加载下一个数据,从缓存池中取出空闲的cell
static NSString *reuseID = @"ID";//每次调用的时候防止都会创建对象,所以延长对象的生命周期
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseID];
if(cell == nil) {
cell = [[UITableViewCellalloc] initWithStyle:UITableViewCellStyleSubtitlereuseIdentifier:reuseID];
//不能再次设置cell的属性
}