문제

How can I calculate height of a custom UITableViewCell by using UILabel's content in table view cell?

도움이 되었습니까?

해결책

Implement your heightForRowAtIndexPath like this:

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

    NSString *text = [dataArray objectAtIndex:indexPath.row]; //your data string

    CGSize constraint = CGSizeMake(yourLabel.frame.size.width, 2000.0f);
    CGSize size;

    NSStringDrawingContext *context = [[NSStringDrawingContext alloc] init];
    CGSize boundingBox = [text boundingRectWithSize:constraint 
                                            options:NSStringDrawingUsesLineFragmentOrigin 
                                         attributes:@{NSFontAttributeName:yourLabel.font}
                                            context:context].size;

    size = CGSizeMake(ceil(boundingBox.width), ceil(boundingBox.height));

    return size.height;
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top