我正在尝试使用占位符从远程源在标准 UITableViewCell
上设置 imageView
。无论使用 SDWebImage
还是 AlamofireImage
作为库,我都会遇到这个问题。
这是初始加载后单元格的显示方式:
然后,一旦单元格被点击/重新加载,它就会显示为:
在 Storyboard中,我将行高调整为 60 点,而不是标准的 44 点。如果我将高度恢复为标准的 44 点,则问题不再存在。
我认为这个问题更多地与单元格的自定义高度有关,而不是与提供占位符的库有关。我在 AlamofireImage in case it was a problem with the library 上做了一个问题但事实并非如此。
我怎样才能让图像最初设置为正确的大小,或者防止在重新加载时发生调整大小?
这是我的 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"))
}
}
我现在还在 TableView Controller 上设置了以下覆盖,但它没有任何效果。
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return 60;
}
override func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return 60;
}
最佳答案
我想发表评论,但我没有足够的声誉来发表评论。因此,“答案”。
我记得以前遇到过这个问题,这是因为我没有在我的 UITableViewCell XIB 上为它设置适当的约束。一旦我将它设置为固定的宽度/高度并将其固定到父 View 的边缘,问题就自行解决了。
关于ios - 防止 UITableViewCell ImageView 在点击时改变大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36414744/