iOS UITableView的用法

什么是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的属性

}












  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值