无法根据textView动态增加UITableViewCell hight

时间:2016-08-01 13:14:57

标签: ios objective-c xcode autolayout xib

我有一个带文字视图的自定义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 个答案:

答案 0 :(得分:0)

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

第1步

Autolayoutstoryboard中使用.xib,并确保您对动态标签的约束应该是这样的。

enter image description here

第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的{​​{1}}(此处我猜是description_lbl)应为numberOfLines

希望它适合你。 :)

快乐的编码!!