UITableViewCell,具有动态高度的UITextView [英] UITableViewCell, UITextView with dynamic height

查看:116
本文介绍了UITableViewCell,具有动态高度的UITextView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要创建一个包含大量文本的 UITableViewCell 。我知道我可以在我的单元格中添加 UITextView ,但每个条目的文本数量都不一样。

I need to make a UITableViewCell that holds alot of text. I know I can add a UITextView to my cell, but each entry will not have the same amount of text.

我知道我可以控制 UITableViewCell 的高度: - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath ,但这不是我想要的。

I know I can control the height of the UITableViewCell with: -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath, but thats not really what I am looking for.

情景1:

 ---------------
| First Cell    |
 ---------------
 ---------------
| Second Cell   |
| with some     |
| text.         |
 ---------------
 ---------------
| Third Cell    |
 ---------------

场景2:

 ---------------
| First Cell    |
 ---------------
 ---------------
| Second Cell   |
| with some     |
| more text and |
| an unknown    |
| cell height.  | 
 ---------------
 ---------------
| Third Cell    |
 ---------------


推荐答案

使用 UILabel 获取您的手机短信。然后,您可以使用 sizeWithFont:constrainedToSize:来计算每个单元格内UILabel的高度。例如:

Use UILabel for your cell text. You can then use sizeWithFont:constrainedToSize: to calculate the height of that UILabel within each cell. For example:

#define PADDING 10.0f

- (CGFloat)tableView:(UITableView *)t heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *text = [self.items objectAtIndex:indexPath.row];
    CGSize textSize = [text sizeWithFont:[UIFont systemFontOfSize:14.0f] constrainedToSize:CGSizeMake(self.tableView.frame.size.width - PADDING * 3, 1000.0f)];

    return textSize.height + PADDING * 3;
}

这篇关于UITableViewCell,具有动态高度的UITextView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆