根据UILable高度让UITableViewCell自动适应高度
来源:互联网 发布:手机阿里云登录 编辑:程序博客网 时间:2024/06/06 22:13
根据UILable高度让UITableViewCell自动适应高度
UIKIT提供的UITableView 很强大,但其单元格并没有提供自动适应内容高度的属性设置,在开发自定义的数据列表时显得非常不灵活。目前常用的做法是根据Cell文字的数量,然后根据文字的字体,算出需要占用的空间,最终得出占用空间高度。这种情况适合于纯文本的UITableViewCell。但如果UITableViewCell中本身就包括很多子视图,通过计算文字的形式是不能满足需求的。具体的实现方法请查看该文章:http://beauty-soft.net/blog/ceiba/Ios/20130326/607.html
接下来将使用另外一种方式,实现动态计算UITableViewCell的高度。在原理上与计算文字的形式相似,不同的是这里将通过在UITableViewCell中插入一个UILable,该视图控件作为Cell的根控件,我们在自定义文字、图片时均需要通过自定义的视图插入到根UILabel中。最后通过计算根UILable控件的高度,得到所需要的UITableViewCell的高度。代码如下。
//表格单元格数据- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ NSString *identifier=@"mycell"; UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:identifier]; NSInteger row=(indexPath.row)-1; if (row<=0) row=0; UILabel *label = nil; if (cell==nil) { cell =[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier]; [cell autorelease]; //Cell根UILable label = [[UILabel alloc] initWithFrame:CGRectZero]; label.tag = 1; label.lineBreakMode = UILineBreakModeWordWrap; label.highlightedTextColor = [UIColor whiteColor]; label.numberOfLines = 0; label.opaque = NO; // 选中Opaque表示视图后面的任何内容都不应该绘制 label.backgroundColor = [UIColor clearColor]; [cell.contentView addSubview:label]; [label release]; } NSArray *rotStr=[dic_ objectForKey:@"rows"]; NSMutableDictionary *strs=[rotStr objectAtIndex:row]; NSString *text=[strs objectForKey:@"mesatecontent"]; CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f); CGRect cellFrame = [cell frame]; cellFrame.origin = CGPointMake(0, 0); CGRect rect = CGRectInset(cellFrame, 2, 2); label.frame = rect; CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap]; if (!label) label = (UILabel*)[cell viewWithTag:1]; [label sizeToFit]; [label setFrame:CGRectMake(0, 0, [self screenWidth], MAX(size.height+50, 44.0f))]; //[label setText:text]; //头像 UIImageView *headImaegView=[[UIImageView alloc] init]; headImaegView.frame=CGRectMake(2, 2, 45, 45); [headImaegView setImageWithURL:[NSURL URLWithString:@"http://t.beauty-soft.net/images/headimg/ceiba1343114887_lisv.jpg"] placeholderImage:[UIImage imageNamed:@"nopic.jpg"]]; [cell.contentView addSubview:headImaegView]; [headImaegView release]; //发表用户 UILabel *titleLabel=[[UILabel alloc] init]; titleLabel.frame=CGRectMake(53, 0, [self appWidth]-53, 20); titleLabel.text=[strs objectForKey:@"adduser_nickname"]; [titleLabel setFont:[UIFont systemFontOfSize:FONT_SIZE]]; [label addSubview:titleLabel]; NSLog(@"高度====%f",MAX(size.height, 44.0f)); //正文 messageLabel = [[UILabel alloc] initWithFrame:CGRectMake(53, 20, [self appWidth]-53, MAX(size.height, 44.0f))]; [messageLabel setText:text]; [messageLabel setNumberOfLines:0]; [messageLabel sizeToFit]; [messageLabel setLineBreakMode:UILineBreakModeWordWrap]; [messageLabel setFont:[UIFont systemFontOfSize:FONT_SIZE]]; [label addSubview:messageLabel]; [messageLabel release]; //在UILabel中添加更多UIVew //……………………………………………………………………………… //计算UILabel高度 float cellHeight=label.frame.size.height; if (label.frame.size.height<50.0f) { cellHeight=50; } cellFrame.size.height = 10 + cellHeight; [cell setFrame:cellFrame]; return cell; }
在上述代码中,自定视图控件的宽度都减少53,这是为了腾出空间给左边头像区域的。在实现应用开发时,可根据需要设置。
在返回Cell高度时,直接获取根据当前的索引号计算相应的Cell高度即可。
//单元格高度- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath]; return cell.frame.size.height;}
0 0
- 根据UILable高度让UITableViewCell自动适应高度
- UITableViewCell根据内容自动高度
- UITableViewCell根据内容自动高度
- 让div自动适应高度
- uilable根据内容多少自动改变高度
- UILable根据内容多少自动改变高度
- UITableViewCell 适应字符串高度
- 如何让iframe自动适应内容高度
- 让div自动适应内容的高度
- 让div自动适应内容的高度
- 如何让iframe自动适应内容高度
- iframe高度自动适应
- frame高度自动适应
- iframe自动适应高度
- iframe 高度自动适应
- Iframe自动适应高度
- iframe高度自动适应
- IFRAME 自动适应高度
- 【设计模式】对象的克隆-原型模式
- ZOJ1101-Gamblers 终于AC。。。
- AngularJS的应用(4)
- 关于四位计数器的设计,阻塞式与非阻塞式赋值引发的问题
- com.mysql.jdbc.CommunicationsException: Communications link failure due to underlying exception:
- 根据UILable高度让UITableViewCell自动适应高度
- ZOJ 3323Somali Pirates
- winfrom中出现" 配置系统未能初始化"
- Android中Parcelable接口用法
- 第一章
- window是系统杀死8080端口的线程
- JavaScript 数据结构
- sqlite错误 The database disk image is malformed database disk image is malformed 可解决
- archlinux - 多系统引导