当文本比平时长时,heightForRowAtIndexPath永远不会显示多行。
下面是我的代码:
#define FONT_SIZE 14.0f
#define CELL_CONTENT_WIDTH 320.0f
#define CELL_CONTENT_MARGIN 10.0f
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
CGFloat result = 44.0f;
CGFloat width = 0;
CGFloat tableViewWidth;
CGRect bounds = [UIScreen mainScreen].bounds;
if (UIInterfaceOrientationIsPortrait(self.interfaceOrientation))
tableViewWidth = bounds.size.width;
else
tableViewWidth = bounds.size.height;
width = tableViewWidth - 110; // fudge factor, 115 isn't quite right
NSUInteger index = [indexPath row];
id doc = [[self displayedObjects] objectAtIndex:index];
NSString *title = [doc title];
if (title)
{
// The notes can be of any height
// This needs to work for both portrait and landscape orientations.
// Calls to the table view to get the current cell and the rect for the
// current row are recursive and call back this method.
CGSize textSize = { width, 20000.0f }; // width and height of text area
CGSize size = [title sizeWithFont:[UIFont systemFontOfSize:14.0f] constrainedToSize:textSize lineBreakMode:UILineBreakModeWordWrap];
size.height += 29.0f; // top and bottom margin
result = MAX(size.height, 44.0f); // at least one row
}
return result;
}任何帮助都将不胜感激。谢谢!
发布于 2011-03-06 02:20:07
若要使label显示多行文字,必须将其numberOfLines属性设置为允许的最大行数(如果是任意行数,则设置为0)。因此,在设置单元格时,在cellForRowAtIndexPath:方法中添加:
...
cell.textLabel.numberOfLines = 0;
...发布于 2011-03-06 02:28:27
我想您不会使用自定义的UITableViewCell。因此,您可能正在使用类似于以下代码的内容来设置标题:
cell.textLabel.text = @"long long long text";要使文本显示在多行上,您必须配置默认的标题UILabel,使其文本显示在多行上。如下所示:
cell.textLabel.numberOfLines = 3;计算行数的方法与计算单元格高度的方法相同。事实上,我认为你必须计算两者才能做你想做的事情(高度适应内容)。
CGSize size = [title sizeWithFont:[UIFont systemFontOfSize:14.0f] constrainedToSize:textSize lineBreakMode:UILineBreakModeWordWrap];在textSize中,重要的是设置width属性的行宽和一个“大”到足以“承载”文本的高度值。
初始化size后,可以将高度除以UILabel行的高度。
https://stackoverflow.com/questions/5205770
复制相似问题