点击时防止UITableViewCell图像视图改变大小

我正在尝试使用占位符从远程源在标准UITableViewCell上设置imageView 。 无论使用SDWebImage还是AlamofireImage作为库,我都有这个问题。

以下是初始加载后单元格的显示方式:

初始加载后图像在单元格中的显示方式

然后,一旦单元格被轻击/重新加载,它就显示为:

点击/重新加载时,图像会缩小并移动文本

在故事板中,我将行高调整为60分而不是标准的44分。 如果我将高度返回到标准44点,则问题不再存在。

我相信这个问题与单元格的自定义高度有关,而不是提供占位符的库。 我在AlamofireImage上遇到了一个问题,如果它是库的问题,但结果却并非如此。

如何最初将图像设置为正确的大小,或者防止重新加载时resize?

这是我的UITableViewCell子类的代码。 请注意,即使我删除layoutSubviews中的框架插入代码,也会出现问题。

 class CategoryTableViewCell: UITableViewCell { var category: Category! { didSet { self.updateCategory(category) } } override func layoutSubviews() { super.layoutSubviews() self.imageView?.frame = CGRectInset(self.imageView!.frame, 10, 10) } private func updateCategory(category: Category) { self.textLabel?.text = category.name self.imageView?.sd_setImageWithURL(category.largeImage ?? nil, placeholderImage: UIImage(named: "DefaultCategory")) } } 

我现在还在表视图控制器上设置了以下覆盖,但它没有任何效果。

 override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {    return 60; }   override func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {    return 60; } 

我想留言,但我没有足够的声誉。 因此,“答案”。

我记得以前碰到这个问题,这是因为我没有在我的UITableViewCell XIB上设置适当的约束。 一旦我将其设置为固定的宽度/高度并将其固定到父视图的边缘,问题就会自行解决。

我正在使用SDWebImage ,这篇post帮我SDWebImage固定宽度的单元格图像 。

这是我的解决方案,只需保存imageView,text和detailText的初始帧,然后在layoutSubviews()上恢复它

 class MoreTableViewCell: UITableViewCell { var imageFrame: CGRect? = nil var textFrame: CGRect? = nil var detailTextFrame: CGRect? = nil override func layoutSubviews() { super.layoutSubviews() if imageFrame == nil { imageFrame = imageView?.frame textFrame = textLabel?.frame detailTextFrame = detailTextLabel?.frame } if imageFrame != nil { self.imageView?.frame = imageFrame! } if textFrame != nil { self.textLabel?.frame = textFrame! } if detailTextFrame != nil { self.detailTextLabel?.frame = detailTextFrame! } } }