- (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;
}