新浪微博单元格自定义高度设置

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

{

    const CGFloat  fontSize = 14.0f;

    //    用户头部信息的高度

    CGFloat height4Header = 40.0f;

    //    微博原文所占的高度

    CGFloat statusTextHeight = 0.0f;

    //    原创微博附带的图片所占的高度

    CGFloat statusImageViewHeight = 0.0f;

    //    转发微博正文所占的高度

    CGFloat reweetStatusTextHeight = 0.0f;

    

    NSDictionary *statusInfo = self.statuesList[indexPath.section];

    NSString *content = statusInfo[kStatusText];

    statusTextHeight = [content frameHeightWithFontSize:fontSize forViewWidth:310.0f];

    

    NSDictionary *reweetStatus = statusInfo[kStatusRetweetStatus];

    if (nil == reweetStatus ) {

        //如果没有转发微博内容, 则计算微博图片的大小

        statusImageViewHeight = [self height4StatusImageView:statusInfo];

    }else

    {

        NSString *retContent = reweetStatus[kStatusText];

        reweetStatusTextHeight = [retContent frameHeightWithFontSize:fontSize forViewWidth:310.0f];

        statusImageViewHeight = [self height4StatusImageView:reweetStatus];

    }

    return (height4Header + statusTextHeight + statusImageViewHeight + reweetStatusTextHeight + 30);

}


//不管是微博正文还是转发微博,图片的高度设置都是一样的

- (CGFloat)height4StatusImageView:(NSDictionary*)dicStatuInfo

{

    CGFloat statusImageViewHeight = 0.0f;

    NSArray *picUrls = dicStatuInfo[kStatusPicUrls];

    if (picUrls.count == 1) {

        NSDictionary *dic = picUrls[0];

        NSString *strPicUrl = dic[kStatusThumbnailPic];

        // 从新浪服务器以同步的方式下载图片数据

        // NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:strPicUrl]];

        // UIImage *image = [UIImage imageWithData:imageData];

        CGSize size = [self downloadJpgImage:strPicUrl];

        statusImageViewHeight += size.height;

    }else

    {

        int picLineCount = ceil(picUrls.count / 3.0);

        statusImageViewHeight += (80*picLineCount);

    }

    return statusImageViewHeight;

    

}


-(CGFloat)frameHeightWithFontSize:(CGFloat)fontsize forViewWidth:(CGFloat)width

{

    //字符串的总宽度

    NSDictionary *attributes = @{NSFontAttributeName: [UIFont systemFontOfSize:fontsize]};

    NSLog(@"attributes:%@",attributes);

    CGSize size = [self sizeWithAttributes:attributes];

    

    //显示的行数

    //每一行显示的字数,使用控件的宽度/字体的大小

    NSUInteger wordsPerLine = floor(width/fontsize);//这样写为了让一行显示完整的字符串

    //每一行显示宽度 字体大小*每一行显示的字数

    CGFloat widthPerLine = fontsize * wordsPerLine;

    

    NSUInteger numLines = ceil(size.width / widthPerLine);

    CGFloat height = numLines * size.height;

    return height;

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值