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

来源:互联网 发布:sas 优化板块 编辑:程序博客网 时间:2024/06/05 15:42

- (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 = [contentframeHeightWithFontSize:fontSize forViewWidth:310.0f];

    

   NSDictionary *reweetStatus = statusInfo[kStatusRetweetStatus];

   if (nil == reweetStatus ) {

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

        statusImageViewHeight = [selfheight4StatusImageView:statusInfo];

    }else

    {

       NSString *retContent = reweetStatus[kStatusText];

        reweetStatusTextHeight = [retContentframeHeightWithFontSize:fontSize forViewWidth:310.0f];

        statusImageViewHeight = [selfheight4StatusImageView: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 = [selfdownloadJpgImage: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: [UIFontsystemFontOfSize:fontsize]};

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

   CGSize size = [selfsizeWithAttributes:attributes];

    

    //显示的行数

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

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

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

   CGFloat widthPerLine = fontsize * wordsPerLine;

    

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

   CGFloat height = numLines * size.height;

   return height;

}


0 0
原创粉丝点击