表格单元格内的TextView无法正确显示uitableview

时间:2014-10-20 20:21:59

标签: ios objective-c uitableview

我现在跟随iOS Apprentice关于地理定位的一些过时的教程。

无论如何,要完成的工作似乎非常简单: 将text view放在table view cell内。在故事板上,一切看起来都很棒,但是当我运行它时,它看起来如下图所示(textview涵盖了下面的类别表格单元格视图项目):

enter image description here

我有以下设置:

enter image description here

将textview保持在表格视图单元格和文本中的最佳方法是什么,并且overllay-y(因此在CSS中调用它 - 我是iOS的新手)将被添加到textview中?

1 个答案:

答案 0 :(得分:0)

我同意@railwayparade你应该使用

cell.textLabel.numberOfLines = 0;

使用heightForRowAtIndexPath计算单元格需要的大小

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // replace "THE_TEXT" with the text you are displaying and "TEXT_LABEL_FONT" with the font you're using in the cell

    CGSize size = ["THE_TEXT" sizeWithFont:"TEXT_LABEL_FONT" constrainedToSize:CGSizeMake(self.table.bounds.size.width, HUGE_VALF) lineBreakMode:NSLineBreakByWordWrapping];
    return size.height;
}

您可能希望继承UITableViewCell并覆盖layoutSubviews以将textLabel设置为完全正确的大小,并根据需要添加填充

-(void)layoutSubviews // in your UITableViewCell subclass
{
    [super layoutSubviews];
    self.textLabel.frame = CGRectMake(0.0f,0.0f,self.contentView.bounds.size.width,self.contentView.bounds.size.height);
}

不要忘记如果你在布局子视图的宽度上添加填充,你必须更改heightForRowAtIndexPath中的约束宽度