根据UILable高度让UITableViewCell自动适应高度

根据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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值