UITableviewCell使用以及自定义高度

UITableView号称是 iOS里面最难使用也是最复杂的一个控件?

是不是暂且不说,反正我觉得HttpRequest也是挺复杂的。


但确实被UItableview折磨了一段时间,还好搞定了一小半。


一、如何重用UITableviewCell

 重用的目的是为了减少内存消耗,假如有1千个cell,如果不重用,那么每一次滑动都得重新

alloc 很多很多的cell,耗费内存,同时屏幕会出现不连续的现象,晃眼睛。

重用cell很简单,在创建cell的时候,使用

alloc initwithtableviewCellStyle:reuseIdentifer这个接口创建cell实例,而非使用alloc initwithFrame

使用前者表示该cell可重用,identifer使用一个固定的NSString即可

       代码如下:

[objc]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. -(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath  
  2. {  
  3.     static NSString *CellIdentifier = @"Cell";  
  4.     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];//首先从可重用队列里面弹出一个cell  
  5.     if (cell == nil) {//说明可重用队列里面并cell,此时需要重新创建cell实例,采用下面方法  
  6.         cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] ;    
  7.     }else{//此时表示有可重用cell,直接返回即可  
  8.         NSLog(@"cell 重用啦");  
  9.     }      
  10.     return cell;  
  11. }  

二、如何如何动态调整cell的高度?

这个问题还是比较头疼的,下面这个函数肯定要用到

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

经过实践之后发现,可以在创建cell或者重用cell的时候,设置其frame

比如cell.frame=CGRectMake(0,0,320,450);

这个代码会有效,同时在下面这个函数里面

  使用:

[objc]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath  
  2. {  
  3.     
  4.     NSLog(@"当前是第%ld行",(long)indexPath.row);  
  5.       
  6.     UITableViewCell *myCell=[self tableView:tableView cellForRowAtIndexPath:indexPath];//获取当前indexPath中的cell实例  
  7.     if( myCell == nil ){  
  8.         return 0;  
  9.           
  10.     }else{  
  11.           
  12.         NSLog(@"%f",myCell.frame.size.height);  
  13.         return  myCell.frame.size.height;  
  14.           
  15.     }  
  16.     return 0;  
  17.       
  18. }  
上面获取当前indexPath的cell实例会重新申请建立一个实例(意思是个cell实际要创建两个实例)

这样的目的是为了获取cell的frame,如果不这样做也可以在第一部分创建cell的时候,将cell的frame保存在一个私有

变量中,在heightForRowAtIndexPath中访问这个私有变量

通过上述方式可以动态改变UITableViewCell的高度


三、对于一个UILabel,根据其内容计算CGRect

首先要设置UILable的font,比如

tmLabel.font=[UIFont  systemFontOfSize:14.0f];

然后使用boundingRectOfSize计算出该尺寸对应的矩形大小,代码如下:

[objc]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. NSDictionary *attrDic=@{NSFontAttributeName:[UIFont systemFontOfSize:12.0f]};  
  2. CGSize labelSize=[text boundingRectWithSize:CGSizeMake(320990)  
  3.                                     options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading  
  4.                                  attributes:attrDic context:nil].size;  
  5.   
  6. CGRect labelRect=CGRectMake(00, labelSize.width, labelSize.height);  
现在UILable的rect都可以被计算出来了,那么如果自定义一个UITableViewCell,并且其内部的UILabel高度可变的话

也是可以实现的



四、内部含有可变高度的UILabel的UITableViewCell

如果还有其他控件,比如UIButton等等,也是一样的。

这些控件在实例化的时候,设置frame为CGRectZero, 然后分别计算各自的高度和尺寸

使用cell.contentview addSubview 的方式,将这些子空间添加到cell中。重新计算cell的frame时

也需要把这些控件的frame累加上。上代码:

[objc]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. -(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath  
  2. {  
  3.   
  4.     static NSString *CellIdentifier = @"Cell";  
  5.     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];  
  6.       
  7.     if (cell == nil) {  
  8.         cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] ;  
  9.           
  10.         UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];  
  11.         label.tag = 1;  
  12.         label.lineBreakMode = NSLineBreakByCharWrapping;  
  13.         label.highlightedTextColor = [UIColor whiteColor];  
  14.         label.numberOfLines = 0;  
  15.         label.opaque = NO// 选中Opaque表示视图后面的任何内容都不应该绘制  
  16.         label.font=[UIFont systemFontOfSize:12.0f];  
  17.         label.backgroundColor = [UIColor grayColor];  
  18.         [cell.contentView addSubview:label];  
  19.           
  20.         UIButton *tmpButton=[[UIButton alloc]initWithFrame:CGRectZero];  
  21.         tmpButton.tag=2;  
  22.         tmpButton.backgroundColor=[UIColor cyanColor];  
  23.         [cell.contentView addSubview:tmpButton];  
  24.         tmpButton.opaque=NO;  
  25.         [tmpButton setTitle:@"nihao" forState:UIControlStateNormal];  
  26.         [tmpButton setTitleColor:[UIColor whiteColor ]forState:UIControlStateHighlighted];  
  27.         [tmpButton addTarget:self action:@selector(tmpButtonHandler:) forControlEvents:UIControlEventTouchUpInside];  
  28.           
  29.     }else{  
  30.         NSLog(@"cell 重用啦");  
  31.     }  
  32.       
  33.         UILabel *tmpLabel=(UILabel *)[cell viewWithTag:1];  
  34.         NSString *text=[tmpArray objectAtIndex:indexPath.row];  
  35.         tmpLabel.text=text;  
  36.           
  37.         UIButton *tmpButton=(UIButton *)[cell viewWithTag:2];  
  38.           
  39.         NSDictionary *attrDic=@{NSFontAttributeName:[UIFont systemFontOfSize:12.0f]};  
  40.         CGSize labelSize=[text boundingRectWithSize:CGSizeMake(320990)  
  41.                                             options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading  
  42.                                          attributes:attrDic context:nil].size;  
  43.           
  44.         CGRect labelRect=CGRectMake(00, labelSize.width, labelSize.height);//计算UILabel的rect  
  45.         [tmpLabel setFrame:labelRect];  
  46.         [tmpButton setFrame:CGRectMake(0, labelSize.height+110050)];//计算UIButton 子控件的rect  
  47.         [cell setFrame:CGRectMake(00, labelSize.width, labelSize.height+50+1)];//cell的frame是以上两个子控件之和  
  48.     
  49.     return cell;  
  50. }  

为何不再创建时设置frame,而是在if和else逻辑后面?

重用cell的时候,从重用cell队列里面取出的cell,其内容(UILabel)是之前的cell内容,需要重新填充UILabel并且重新计算

整个cell的frame并设置其frame。而创建cell的时候也需要设置frame,所以这两个逻辑重复,直接放在if else逻辑外面做。

下面是动态改变UITableViewCell的高度图:




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值