【问题标题】:Autolayout is breaking when UITableViewCell is scrolled especially for UIImageView滚动 UITableViewCell 时自动布局中断,尤其是对于 UIImageView
【发布时间】:2018-02-10 03:03:41
【问题描述】:

我需要 UITableViewCell 的动态高度取决于内容长度。

这是一个原型细胞

第一次加载时内容显示正常

但是当我滚动 UITableView 时,单元格的内容会像这样中断,尤其是 UIImageView。

在cell的实现中,我只使用了setImagesetText方法。

似乎有什么问题?或者,在viewDidLoad 之后是否有另一种方法可以不同地设置每个单元格的高度?

** 编辑:这是我对cellForRowAtIndexPath的实现

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NotificationCell *cell = [tableView dequeueReusableCellWithIdentifier:@"notification"];
    if(!cell) {
        [tableView registerNib:[UINib nibWithNibName:@"NotificationCell" bundle:nil] forCellReuseIdentifier:@"notification"];
        cell = [tableView dequeueReusableCellWithIdentifier:@"notification"];
    }

    [cell setContent:[notifications objectAtIndex:indexPath.row]];
    [cell layoutIfNeeded];
    return cell;
}

NotificationCell.m 中的setContent 只有setTextsetImage

【问题讨论】:

  • 正确配置了动态高度步长???
  • 将 [cell layoutIfNeeded] 放在返回单元之前
  • 能否显示单元格文件,以及tableView cellForRowAtIndex 路径方法?
  • @iamirzhan 我编辑了帖子,但我认为我做得很好。
  • @AlfredWoo 是的,谢谢!这可能是愚蠢的建议,但是您是否为您的 imageView 创建了出口?因为 UITableView 已经有了 imageView 属性,而且看起来你将图像设置为那个 imageView,因为它在单元格的左侧。

标签: ios uitableview autolayout


【解决方案1】:

因为 tableView 已经有一个 imageView 属性并且它位于单元格的左侧,所以您将图像设置为该 imageView。您应该重命名自定义 imageView 的出口。所以,它不应该叫 imageView,给它取另一个名字。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-07-01
    • 2014-01-17
    • 2015-04-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多