防止 UITableViewCell 图像视图在点击时改变大小 [英] Prevent UITableViewCell image view from changing size when tapped
问题描述
我正在尝试使用占位符从远程源设置标准 UITableViewCell
上的 imageView
.无论使用 SDWebImage
还是 AlamofireImage
作为库,我都会遇到这个问题.
I'm trying to set the imageView
on a standard UITableViewCell
from a remote source using a placeholder. I have this issue regardless of using SDWebImage
or AlamofireImage
as the library.
这是初始加载后单元格的显示方式:
Here is how the cell appears after the initial load:
然后,一旦单元格被点击/重新加载,它就会显示为:
Then, once the cell has been tapped/reloaded it appears as this:
在故事板中,我将行高调整为 60 点,而不是标准的 44 点.如果我将高度恢复到标准的 44 点,则问题不再存在.
In the storyboard, I've adjusted the row height to be 60 points instead of the standard 44 points. If I return the height to the standard 44 points, the issue is no longer present.
我认为这个问题更多地与单元格的自定义高度有关,而不是与提供占位符的库有关.我在 AlamofireImage 上提出了一个问题,以防图书馆出现问题 但它变成了事实并非如此.
I believe this issue is more related to custom height of the cell rather than the library providing the placeholder. I made an issue on AlamofireImage in case it was a problem with the library but it turns out this is not the case.
如何在初始时将图像设置为正确的大小,或防止在重新加载时发生大小调整?
How can I have the image set at the right size initially, or prevent a resize from occurring upon reload?
这是来自我的 UITableViewCell
子类的代码.请注意,即使我删除 layoutSubviews
内的框架插入代码,也会出现问题.
Here is the code from my UITableViewCell
subclass. Note that the issue occurs even if I remove the frame inset code inside of 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"))
}
}
<小时>
我现在还在表视图控制器上设置了以下覆盖,但它没有任何效果.
I've now also set the following overrides on the table view controller, bu it hasn't had any effect.
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return 60;
}
override func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return 60;
}
推荐答案
我正在使用 SDWebImage,这篇文章帮助了我 SDWebImage 固定宽度单元格图像一>.
I'm using SDWebImage, and this post helped me SDWebImage fixed width cell images.
这里是我的解决方案,只需要保存imageView的初始帧,text和detailText,在layoutSubviews()上恢复
Here is my solution, just save the initial frame of the imageView, text and detailText, and restore it on the 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!
}
}
}
这篇关于防止 UITableViewCell 图像视图在点击时改变大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!