如何根据表中的内容设置行高?

Dev*_*shi 6 cocoa nstableview

通常,表具有固定的行高,但根据我的要求,我需要根据其中的内容设置每行的高度.

有人能建议我一些解决方案吗?

谢谢,

Miraaj

dj2*_*dj2 5

表视图委托协议有一个tableView:heightOfRow,它允许您设置表视图中每行的高度.

  • Miraaj:您可以询问每列的宽度,并且可以询问字符串如果绘制成给定大小的矩形(例如,一列具有列的宽度和非常大的高度),它将占用多少空间.http://developer.apple.com/mac/library/documentation/Cocoa/Reference/ApplicationKit/Classes/NSString_AppKitAdditions/Reference/Reference.html#//apple_ref/occ/instm/NSString/boundingRectWithSize:options:attributes:对于图像,你将不得不自己做数学,使用图像大小的宽高比​​. (2认同)

Dev*_*shi 1

感谢大家的建议和帮助。现在使用以下代码解决了这个问题tableView:heightOfRow:

float colWidth = [[[tableView tableColumns] objectAtIndex:1]width];

NSString *content = [[[tempArray objectAtIndex:row] objectForKey:@"tValue"] string];

float textWidth = [content sizeWithAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[NSFont fontWithName:@"Lucida Grande" size:15],NSFontAttributeName ,nil]].width;

float newHeight = ceil(textWidth/colWidth);

newHeight = (newHeight * 17) + 13;
if(newHeight < 47){
    return 47;
}   
return newHeight;
Run Code Online (Sandbox Code Playgroud)