无法根据 textView 动态增加 UITableViewCell 高度

Unable to increase UITableViewCell hight according to textView dynamically

我有一个带有文本视图的自定义 UITableViewCell

textView 的高度根据其文本增加。

我不知道如何增加单元格高度以适应文本视图。

这是我的代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NotificationCell *cell = (NotificationCell *)[tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (cell == nil) {
        NSArray *ary = [[NSBundle mainBundle]loadNibNamed:@"NotificationCell" owner:self options:nil];
        cell = [ary objectAtIndex:0];

    }
    NSMutableDictionary *dict =  [[NSMutableDictionary alloc] initWithDictionary:[NotificationArray objectAtIndex:indexPath.section]];
    cell.title_lbl.text=[dict objectForKey:@"event_title"];
    cell.description_lbl.text=[dict objectForKey:@"description"];
    [cell.description_lbl sizeToFit];
    [cell sizeToFit];

    return cell;
}

为此,您需要按照以下步骤操作。

步骤 1

在您的 storyboard.xib 中使用 Autolayout 并确保您对动态标签的约束应该像这样。

步骤 2

在 viewcontroller 的 viewDidLoad 上,您必须像这样提供 estimatedRowHeight

self.tblViewOutlet.rowHeight = UITableViewAutomaticDimension;
self.tblViewOutlet.estimatedRowHeight = 50.0; //any height which is like minimum

步骤 3

并在 tableView 委托 heightForRowAtIndexPath 中,

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewAutomaticDimension;
}

确保你的 UILabel(这里我猜是 description_lbl)的原型单元 属性 numberOfLines 应该是 0.

希望对你有用。 :)

编码愉快!!