iOS 7中UITableViewCell和UITextView的高度问题?

121

如何在iOS 7中计算UITableViewCell中带有UITextView的高度?

我在类似的问题上找到了很多答案,但是每个解决方案都涉及到sizeWithFont:方法,而这个方法已经被弃用了!

我知道我必须使用- (CGFloat)tableView:heightForRowAtIndexPath:方法,但是如何计算我的TextView需要显示整个文本的高度呢?

12个回答

0
UITextView *txtDescLandscape=[[UITextView alloc] initWithFrame:CGRectMake(2,20,310,2)];

    txtDescLandscape.editable =NO;
    txtDescLandscape.textAlignment =UITextAlignmentLeft;
    [txtDescLandscape setFont:[UIFont fontWithName:@"ArialMT" size:15]];
    txtDescLandscape.text =[objImage valueForKey:@"imgdescription"];
    txtDescLandscape.text =[txtDescLandscape.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    [txtDescLandscape sizeToFit];
    [headerView addSubview:txtDescLandscape];

    CGRect txtViewlandscpframe = txtDescLandscape.frame;
    txtViewlandscpframe.size.height = txtDescLandscape.contentSize.height;
    txtDescLandscape.frame = txtViewlandscpframe;

我认为这样做可以计算您的文本视图的高度,然后根据该高度调整您的表格视图单元格大小,以便在单元格上显示完整的文本。

-2

对于 iOS 8 及以上版本,您可以直接使用

your_tablview.estimatedrowheight= minheight,您想要的最小高度

your_tableview.rowheight=UItableviewautomaticDimension

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接