iOS动态单元格高度基于非动态文本视图基于非文本

iOS动态单元格高度基于非动态文本视图基于非文本,ios,uitableview,uitextview,tableview,frame,Ios,Uitableview,Uitextview,Tableview,Frame,我正试图让我的UITableViewCell根据textview正确调整大小,该视图根据其文本动态调整大小。但是我不能让他们一起工作。以下是我到目前为止的情况?目前单元格高度正确,但未根据contentSize.height调整textView的大小 - (void)viewDidLoad { [super viewDidLoad]; // Set the label properties! self.titleLabel.text = self.releaseTitl

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

- (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;
}

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

- (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)
然后通过将恒定高度与可变标签高度相加来返回高度

此外,为了保持一致,您应该使用相同的方法在
单元格中为rowatinexpath
设置帧,而不是使用
sizetofit多行线

- (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更改此选项。

试试看,它经过测试,工作正常。我不想为UITableViewCell子类化,尤其是在iOS7出现的情况下……调用ViewWillDisplay:和ViewDidDisplay:时别忘了添加[super ViewWillDisplay:动画]和[super ViewDidDisplay:动画];)能否尝试在contentTextView中调用sizeToFit方法?不,这不起作用。sizeWithFont:现在已不推荐使用。您必须使用BoundingRectize。请参阅我的更新问题:@Jon Erickson提及您将要部署的IOS的确切版本。在iOS7中,sizeWithFont已被弃用。