我正在尝试在我的UITableViewCell
中放置一个用故事板创建的评论。该视图的大小取决于要显示的注释数和每个注释主体的大小。视图在我的cellForRowAtIndexPath:
方法中设置,并调用此函数:
-(void)setupWithComments:(NSArray*)comments count:(int)count {
int commentsToDisplay = comments.count;
if (commentsToDisplay == 0) {
return;
}
CGFloat yValue = 0;
for (int i = commentsToDisplay - 1; i >= 0 ; i--) {
PComment *comment = [comments objectAtIndex:i];
UIView *containerBlock = [[UIView alloc] init];
UILabel *timeAgoLabel = [[UILabel alloc] init];
timeAgoLabel.text = [PUtilities timeAgo:comment.createdAt];
timeAgoLabel.font = [UIFont systemFontOfSize:12.0];
timeAgoLabel.textAlignment = NSTextAlignmentRight;
timeAgoLabel.textColor = [UIColor whiteColor];
[containerBlock addSubview:timeAgoLabel];
[timeAgoLabel makeConstraints:^(MASConstraintMaker *make) {
make.bottom.equalTo(containerBlock.bottom).with.offset(-10);
make.right.equalTo(containerBlock.right).with.offset(-10);
}];
NSAttributedString *commentString = [PUtilities attributeSubstrings:@[comment.formattedBody, comment.creator.username]
withAttributes:@[CommentBodyAttributes, UsernameAttributes]
inFormatString:[NSString stringWithFormat:@"%@ %@", comment.creator.username, comment.formattedBody]];
CGRect commentSize = [comment.attributedBody boundingRectWithSize:CGSizeMake(270, 9999)
options:(NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading)
context:nil];
UILabel *commentLabel = [[UILabel alloc] init];
commentLabel.numberOfLines = 0;
commentLabel.lineBreakMode = NSLineBreakByWordWrapping;
commentLabel.attributedText = commentString;
commentLabel.tag = (i + 1);
[containerBlock addSubview:commentLabel];
[commentLabel makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(containerBlock.left).with.offset(25);
make.top.equalTo(containerBlock.top).with.offset(10);
make.right.equalTo(containerBlock.right).with.offset(-25);
}];
CGFloat height = CGRectGetHeight(commentSize) + 20;
containerBlock.frame = CGRectMake(0, yValue, 320, height);
yValue += height;
[PUtilities logFrame:containerBlock.frame];
[self addSubview:containerBlock];
}
}
挑战在于使表格视图单元格的大小正确。默认情况下,它们应该是帧的大小,减去状态栏(548)。我试图用这种方法获得细胞的正确高度:
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
NSDictionary *CommentBodyAttributes = @{
NSFontAttributeName: [UIFont fontWithName:@"HelveticaNeue" size:12.0],
NSForegroundColorAttributeName: [UIColor whiteColor]
};
PVideo *video = [self.videos objectAtIndex:indexPath.section];
if (video.mostRecentComments.count != 0) {
__block CGFloat height = 0;
[video.mostRecentComments enumerateObjectsUsingBlock:^(PComment *comment, NSUInteger index, BOOL *stop) {
NSAttributedString *commentString = [PUtilities attributeSubstrings:@[comment.formattedBody, comment.creator.username]
withAttributes:@[CommentBodyAttributes, CommentBodyAttributes]
inFormatString:[NSString stringWithFormat:@"%@ %@", comment.creator.username, [comment formattedBody]]];
CGRect commentSize = [commentString boundingRectWithSize:CGSizeMake(270, 9999)
options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading)
context:nil];
height += CGRectGetHeight(commentSize) + 20;
}];
NSLog(@"%f", height + 457);
return 457 + height;
}else {
return kVideoCellHeight;
}
}
但它从来没有适当的大小,导致评论被切断,或底部的空间过多。我刚刚遗漏的代码中是否有明显的错误?