简体   繁体   English

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

[英]Prevent UITableViewCell image view from changing size when tapped

I'm trying to set the imageView on a standard UITableViewCell from a remote source using a placeholder.我试图设置imageView一个标准UITableViewCell使用占位符远程源。 I have this issue regardless of using SDWebImage or AlamofireImage as the library.无论使用SDWebImage还是AlamofireImage作为库,我都有这个问题。

Here is how the cell appears after the initial load:以下是初始加载后单元格的显示方式:

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

Then, once the cell has been tapped/reloaded it appears as this:然后,一旦单元格被点击/重新加载,它就会显示如下:

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

In the storyboard, I've adjusted the row height to be 60 points instead of the standard 44 points.在故事板中,我将行高调整为 60 点,而不是标准的 44 点。 If I return the height to the standard 44 points, the issue is no longer present.如果我将高度恢复到标准 44 点,则问题不再存在。

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.我在AlamofireImage上提出了一个问题,以防它是库的问题,但事实证明并非如此。

How can I have the image set at the right size initially, or prevent a resize from occurring upon reload?如何将图像最初设置为正确的大小,或防止在重新加载时发生调整大小?

Here is the code from my UITableViewCell subclass.这是我的UITableViewCell子类的代码。 Note that the issue occurs even if I remove the frame inset code inside of layoutSubviews .请注意,即使我删除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;
}

I would like to leave a comment but I don't have enough reputation to do so.我想发表评论,但我没有足够的声誉这样做。 Hence, the "answer".因此,“答案”。

I remember running into this problem before and it was due to me not setting proper constraints for it on my UITableViewCell XIB.我记得之前遇到过这个问题,这是因为我没有在 UITableViewCell XIB 上为其设置适当的约束。 Once I set it to a fixed width/height and pin it to the edges of the parent view, the problem resolved itself.一旦我将它设置为固定的宽度/高度并将其固定到父视图的边缘,问题就会自行解决。

I'm using SDWebImage , and this post helped me SDWebImage fixed width cell images .我正在使用SDWebImage ,这篇文章帮助我SDWebImage 固定宽度单元格图像

Here is my solution, just save the initial frame of the imageView, text and detailText, and restore it on the layoutSubviews()这是我的解决方案,只需要保存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!
        }
    }
} 

Just add cell.setNeedsLayout() after setting Cell's image view!设置 Cell 的图像视图后,只需添加cell.setNeedsLayout()

MenuController.shared.fetchImage(url: item.imageURL) { (image) in
        if let image = image {
            DispatchQueue.main.async {
                cell.imageView?.image = image
                cell.setNeedsLayout()
            }
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM