【发布时间】:2011-02-01 16:34:46
【问题描述】:
我有一个 UITableView 和一个使用 Interface Builder 设计的 Cell。 我在那里有两个标签。 问题是,其中一个标签是可以是 500 字或 10 字的文本。
所以我需要一个动态的单元格高度。我该如何处理,否则我有空白空间或单元格高度不够。
也许有人知道如何做到这一点?
最好的问候。
【问题讨论】:
标签: iphone objective-c
我有一个 UITableView 和一个使用 Interface Builder 设计的 Cell。 我在那里有两个标签。 问题是,其中一个标签是可以是 500 字或 10 字的文本。
所以我需要一个动态的单元格高度。我该如何处理,否则我有空白空间或单元格高度不够。
也许有人知道如何做到这一点?
最好的问候。
【问题讨论】:
标签: iphone objective-c
要知道你的文本的大小,你可以使用这个方法:
- (CGFloat)measureTextHeight:(NSString*)text fontName:(NSString*)fontName fontSize:(CGFloat)fontSize constrainedToSize:(CGSize)constrainedToSize {
CGSize mTempSize = [text sizeWithFont:[UIFont fontWithName:fontName size:fontSize] constrainedToSize:constrainedToSize lineBreakMode:UILineBreakModeWordWrap];
return mTempSize.height; }
然后你可以设置你的尺寸单元格:
-(CGFloat)tableView:(UITableVIew*)tableView heightForRowAtIndexPath(NSIndexPath *)indexPath;
【讨论】:
constrainedToSize是什么?
-(CGFloat)tableView:(UITableVIew*) tableView heightForRowAtIndexPath(NSIndexPath *) indexPath
{
//here u can check the row number and ur labels and return ur custom height.
}
【讨论】: