【问题标题】:UIImageView in a UITableViewCell and auto layout not working on cell reuseUITableViewCell 中的 UIImageView 和自动布局不适用于单元重用
【发布时间】:2015-04-22 22:11:08
【问题描述】:

细胞在启动时看起来是正确的。

一旦您滚动到一个新单元格(显然 dequeReusableCell 被调用),该单元格上先前存在的约束就会丢失。

这是单元格中 imageView 的约束设置:

这是应用启动时的样子(单元格按我的意愿布局)

当您开始滚动时,单元格的重复使用会破坏约束。

在单元格上设置图像后,我在 updateImage 方法中调用 layoutIfNeeded():

 func updateWithImage(image: UIImage) {

    userGroupPhotoImageView.layer.masksToBounds = true;
    userGroupPhotoImageView.layer.cornerRadius = 5.0;

    self.userGroupPhotoImageView.image = image
    self.layoutIfNeeded()
}

indexPath 处的行的单元格:

 override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier) as MyCell
    cell.nameLabel?.text = "Cell \(indexPath.row)"
    switch (indexPath.row) {
    case 0:
        cell.updateWithImage(UIImage(named: "group0")!)
    case 1:
        cell.updateWithImage(UIImage(named: "group1")!)
    case 2:
        cell.updateWithImage(UIImage(named: "group2")!)
    case 3:
        cell.updateWithImage(UIImage(named: "group3")!)
    case 4:
        cell.updateWithImage(UIImage(named: "group4")!)
    case 5:
        cell.updateWithImage(UIImage(named: "IMG_0184")!)
    default:
        cell.updateWithImage(UIImage(named: "IMG_0185")!)
    }

    return cell
}

开始滚动后的样子:

向下滚动:

向上滚动到顶部;

ImageView 的约束:

【问题讨论】:

  • 我不清楚你为什么使用 updateWithImage: 方法。前两行只需要执行一次,最好在单元格的初始化代码中完成(并且您根本不需要 layoutIfNeeded 行)。
  • 所以需要在 prepareForReuse() 上将 imageView.image 设置为 nil 我想知道为什么会这样。

标签: ios objective-c iphone uitableview swift


【解决方案1】:

每次设置图像后都需要调用 setNeedsLayout 和 layoutIfNeeded,并且每次重复使用时必须将 ImageView.image 设置为 nil。 :|

class UserGroupCell: UITableViewCell {

@IBOutlet weak var userGroupNameLabel: UILabel!
@IBOutlet weak var userGroupPhotoImageView: UIImageView! {
    didSet {
        userGroupPhotoImageView.clipsToBounds = true;
        userGroupPhotoImageView.layer.masksToBounds = true;
        userGroupPhotoImageView.layer.cornerRadius = 5.0;
    }
}

override func prepareForReuse() {
    super.prepareForReuse()
    userGroupPhotoImageView.image = nil
}

func updateWithImage(image: UIImage) {

    self.userGroupPhotoImageView.image = image
    self.setNeedsLayout()
    self.layoutIfNeeded()
}
}

【讨论】:

  • 我不确定你的应用发生了什么,因为我复制了你的代码,我没有看到和你一样的问题。我确实在控制台中收到了一些约束警告,但是如果我只是删除 layoutIfNeeded 调用(从您的原始代码中),这些警告就会消失。您真的不需要调用 setNeedsLayout 或 layoutIfNeeded,或者将图像设置为 nil。很难从您的图像中看出您有哪些限制;你的图像视图和单元格之间有顶部和底部约束吗?
  • ImageView 上相对于 ContentView 的前导、顶部、尾随、底部。
  • 在我们确定什么是实际问题之前,我将不选中这个答案。顺便说一下上面的限制。
  • 当我在图像视图上设置高度限制时,我在控制台中收到警告。最好删除它,并设置 table view 的 rowHeight 属性来定义高度。
【解决方案2】:

如果您没有找到任何答案。 您应该以编程方式进行。并在

中设置约束
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{

方法。 这样您就可以确定会发生什么。

【讨论】:

    猜你喜欢
    • 2015-08-30
    • 2015-07-01
    • 2014-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多