UITableviewCell使用以及自定义高度
来源:互联网 发布:阿里云域名无法访问 编辑:程序博客网 时间:2024/06/06 09:44
UITableView号称是 ios里面最难使用也是最复杂的一个控件?
是不是暂且不说,反正我觉得HttpRequest也是挺复杂的。
但确实被UItableview折磨了一段时间,还好搞定了一小半。
一、如何重用UITableviewCell
重用的目的是为了减少内存消耗,假如有1千个cell,如果不重用,那么每一次滑动都得重新
alloc 很多很多的cell,耗费内存,同时屏幕会出现不连续的现象,晃眼睛。
重用cell很简单,在创建cell的时候,使用
alloc initwithtableviewCellStyle:reuseIdentifer这个接口创建cell实例,而非使用alloc initwithFrame
使用前者表示该cell可重用,identifer使用一个固定的NSString即可
代码如下:
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];//首先从可重用队列里面弹出一个cell if (cell == nil) {//说明可重用队列里面并cell,此时需要重新创建cell实例,采用下面方法 cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] ; }else{//此时表示有可重用cell,直接返回即可 NSLog(@"cell 重用啦"); } return cell;}
二、如何如何动态调整cell的高度?
这个问题还是比较头疼的,下面这个函数肯定要用到
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
经过实践之后发现,可以在创建cell或者重用cell的时候,设置其frame
比如cell.frame=CGRectMake(0,0,320,450);
这个代码会有效,同时在下面这个函数里面
使用:
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ NSLog(@"当前是第%ld行",(long)indexPath.row); UITableViewCell *myCell=[self tableView:tableView cellForRowAtIndexPath:indexPath];//获取当前indexPath中的cell实例 if( myCell == nil ){ return 0; }else{ NSLog(@"%f",myCell.frame.size.height); return myCell.frame.size.height; } return 0; }上面获取当前indexPath的cell实例会重新申请建立一个实例(意思是个cell实际要创建两个实例)
这样的目的是为了获取cell的frame,如果不这样做也可以在第一部分创建cell的时候,将cell的frame保存在一个私有
变量中,在heightForRowAtIndexPath中访问这个私有变量
通过上述方式可以动态改变UITableViewCell的高度
三、对于一个UILabel,根据其内容计算CGRect
首先要设置UILable的font,比如
tmLabel.font=[UIFont systemFontOfSize:14.0f];
然后使用boundingRectOfSize计算出该尺寸对应的矩形大小,代码如下:
NSDictionary *attrDic=@{NSFontAttributeName:[UIFont systemFontOfSize:12.0f]}; CGSize labelSize=[text boundingRectWithSize:CGSizeMake(320, 990) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:attrDic context:nil].size; CGRect labelRect=CGRectMake(0, 0, labelSize.width, labelSize.height);现在UILable的rect都可以被计算出来了,那么如果自定义一个UITableViewCell,并且其内部的UILabel高度可变的话
也是可以实现的
四、内部含有可变高度的UILabel的UITableViewCell
如果还有其他控件,比如UIButton等等,也是一样的。
这些控件在实例化的时候,设置frame为CGRectZero, 然后分别计算各自的高度和尺寸
使用cell.contentview addSubview 的方式,将这些子空间添加到cell中。重新计算cell的frame时
也需要把这些控件的frame累加上。上代码:
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] ; UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero]; label.tag = 1; label.lineBreakMode = NSLineBreakByCharWrapping; label.highlightedTextColor = [UIColor whiteColor]; label.numberOfLines = 0; label.opaque = NO; // 选中Opaque表示视图后面的任何内容都不应该绘制 label.font=[UIFont systemFontOfSize:12.0f]; label.backgroundColor = [UIColor grayColor]; [cell.contentView addSubview:label]; UIButton *tmpButton=[[UIButton alloc]initWithFrame:CGRectZero]; tmpButton.tag=2; tmpButton.backgroundColor=[UIColor cyanColor]; [cell.contentView addSubview:tmpButton]; tmpButton.opaque=NO; [tmpButton setTitle:@"nihao" forState:UIControlStateNormal]; [tmpButton setTitleColor:[UIColor whiteColor ]forState:UIControlStateHighlighted]; [tmpButton addTarget:self action:@selector(tmpButtonHandler:) forControlEvents:UIControlEventTouchUpInside]; }else{ NSLog(@"cell 重用啦"); } UILabel *tmpLabel=(UILabel *)[cell viewWithTag:1]; NSString *text=[tmpArray objectAtIndex:indexPath.row]; tmpLabel.text=text; UIButton *tmpButton=(UIButton *)[cell viewWithTag:2]; NSDictionary *attrDic=@{NSFontAttributeName:[UIFont systemFontOfSize:12.0f]}; CGSize labelSize=[text boundingRectWithSize:CGSizeMake(320, 990) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:attrDic context:nil].size; CGRect labelRect=CGRectMake(0, 0, labelSize.width, labelSize.height);//计算UILabel的rect [tmpLabel setFrame:labelRect]; [tmpButton setFrame:CGRectMake(0, labelSize.height+1, 100, 50)];//计算UIButton 子控件的rect [cell setFrame:CGRectMake(0, 0, labelSize.width, labelSize.height+50+1)];//cell的frame是以上两个子控件之和 return cell;}
为何不再创建时设置frame,而是在if和else逻辑后面?
重用cell的时候,从重用cell队列里面取出的cell,其内容(UILabel)是之前的cell内容,需要重新填充UILabel并且重新计算
整个cell的frame并设置其frame。而创建cell的时候也需要设置frame,所以这两个逻辑重复,直接放在if else逻辑外面做。
下面是动态改变UITableViewCell的高度图:
- UITableviewCell使用以及自定义高度
- UITableviewCell使用以及自定义高度
- UITableviewCell使用以及自定义高度
- UITableviewCell使用以及自定义高度
- 自定义UiTableViewCell高度
- iOS 动态计算UITableViewCell 高度 以及计算UILable 高度 自定义Cell
- 自定义UITableViewCell类 && 自适应高度
- 32. 自定义UITableViewCell(高度自适应)
- 使用Autolayout定义UITableViewCell高度
- UITableViewCell使用AutoLayout自适应高度
- 使用Xib自定义UITableViewCell
- UITableViewCell以及其子控件自适应高度
- 可任意自定义的UITableViewCell高度
- 通过代码自定义高度不一致的UITableViewCell
- iOS编程------自定义UITableViewCell / cell自适应高度
- UILabel高度的自适应以及UITableViewCell的高度自适应
- 不使用sizeWithFont:minFontSize:actualFontSize:forWidth:使UILabel自适应,以及自定义UITableViewCell的自定义方法
- 使用AutoLayout动态计算UITableViewCell高度
- TableView详细解释
- 支持柴静,我们把代码写的这么漂亮,却呼吸着肮脏的空气,有意思么???
- 2015.02.28侬自语
- 《C语言及程序设计》实践参考——简单循环的流程图
- 9个常用iptables配置实例
- UITableviewCell使用以及自定义高度
- 《C语言及程序设计》实践参考——次数不定的循环的流程图
- 《C语言及程序设计》实践参考——循环的嵌套流程图
- 14寒假集训GeoDefense
- 虚拟机安装之各类虚拟机
- Xcode 的正确打开方式——Debugging
- iptables详解
- Rust、D、Go三者的Pk1
- activity例子