【问题标题】:iOS dynamic cell height based off dynamic textview based off textiOS 动态单元格高度基于基于文本的动态文本视图
【发布时间】:2013-07-23 20:34:18
【问题描述】:

我试图让我的UITableViewCell 根据textview 正确调整大小,该textview 根据其文本动态调整大小。但是我不能让他们一起工作。以下是我到目前为止所拥有的?现在单元格高度是正确的,但 textView 没有根据 contentSize.height 调整大小。

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Set the label properties!
    self.titleLabel.text = self.releaseTitle;
    self.pubDateLabel.text = self.pubDate;
    self.attachmentsLabel.text = [NSString stringWithFormat:@"%lu", (unsigned long)self.attatchments.count];
    self.contentTextView.text = self.summary;    
}

- (void)viewWillAppear:(BOOL)animated
{

}

- (void)viewDidAppear:(BOOL)animated
{
    [self.tableView reloadData];

    // Set the proper height of the content cell of the text
    CGRect frame = self.contentTextView.frame;
    frame.size.height = self.contentTextView.contentSize.height;
    self.contentTextView.frame = frame;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{
    return self.contentTextView.contentSize.height;
}

【问题讨论】:

  • 试试cocoacontrols.com/controls/autoresizingeditabletableviewcell 它已经过测试并且工作正常。
  • 我宁愿不必子类化 UITableViewCell,尤其是在 iOS7 出现时...
  • 在调用 viewWillAppear: 和 viewDidAppear: 时不要忘记添加 [super viewWillAppear:animated] 和 [super viewDidAppear:animated] ;)
  • 能否尝试在 contentTextView 中调用 sizeToFit 方法?
  • 不,这不起作用。

标签: ios uitableview uitextview tableview frame


【解决方案1】:

您需要计算heightForRowAtIndexPath, 中单元格的高度

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *theText=[[_loadedNames objectAtIndex: indexPath.row] name];
CGSize labelSize = [theText sizeWithFont:[UIFont fontWithName: @"FontA" size: 15.0f] constrainedToSize:kLabelFrameMaxSize];
return kHeightWithoutLabel+labelSize.height;

}

你可以通过设置kLabelFrameMaxSize对标签大小进行一些限制

#define kLabelFrameMaxSize CGSizeMake(265.0, 200.0)

然后你通过添加一个恒定高度和可变标签高度来返回高度。

此外,为了保持一致,您应该使用相同的方法在cellForRowAtIndexPath 中设置框架,而不是使用sizeToFitMultipleLines

- (UITableViewCell *) tableView: (UITableView *) tableView cellForRowAtIndexPath: (NSIndexPath *) indexPath
{
FQCustomCell *_customCell = [tableView dequeueReusableCellWithIdentifier: @"CustomCell"];
if (_customCell == nil) {
    _customCell = [[FQCustomCell alloc] initWithStyle: UITableViewCellStyleValue1 reuseIdentifier: @"CustomCell"];
}


_customCell.myLabel.text = [[_loadedNames objectAtIndex: indexPath.row] name];
_customCell.myLabel.font = [UIFont fontWithName: @"FontA" size: 15.0f];

UIView *backView = [[UIView alloc] initWithFrame: CGRectZero];
backView.backgroundColor = [UIColor clearColor];
_customCell.backgroundView = backView;

CGSize labelSize = [_customCell.myLabel.text sizeWithFont:_customCell.myLabel.font constrainedToSize:kLabelFrameMaxSize];
_customCell.myLabel.frame = CGRectMake(5, 5, labelSize.width, labelSize.height);

return _customCell;
}

您可以为 UILabel、UIButton、Cell 更改此项。

【讨论】:

  • sizeWithFont: 现在已弃用。您将不得不使用 boundingRectWithSize。查看我更新的问题:stackoverflow.com/questions/18814755/…
  • @Jon Erickson 提及您要部署的 IOS 的确切版本。
  • 在 iOS7 中 sizeWithFont 已弃用。
猜你喜欢
  • 1970-01-01
  • 2017-07-19
  • 2018-09-30
  • 2012-05-04
  • 1970-01-01
  • 1970-01-01
  • 2016-08-17
  • 1970-01-01
  • 2021-11-25
相关资源
最近更新 更多