【问题标题】:Unable to increase UITableViewCell hight according to textView dynamically无法根据textView动态增加UITableViewCell高度
【发布时间】:2016-12-06 14:20:00
【问题描述】:

我有一个带有文本视图的自定义 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;
}

【问题讨论】:

标签: ios objective-c xcode autolayout xib


【解决方案1】:

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

第 1 步

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

第 2 步

在您的视图控制器的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;
}

确保原型单元格属性numberOfLinesUILabel(这里我猜是description_lbl)应该是0

希望它对你有用。 :)

编码愉快!!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-08
    • 2018-02-10
    • 2011-03-31
    • 1970-01-01
    • 2016-07-27
    • 1970-01-01
    相关资源
    最近更新 更多