ios - 防止 UITableViewCell ImageView 在点击时改变大小

标签 ios swift uitableview

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

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

How the image appears in the cell after the initial load

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

Image shrinks and moves the text when tapped/reloaded

在 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/

相关文章:

iOS移动导航栏箭头

html - UIWeb View : css ignored

arrays - 哪种方法更好,每次保存一个值或评估?

ios - 为什么在调用 `privateManagedObjectContext.perform` 时会发生崩溃(从 com.apple.main-thread(线程 1)排队)?

swift - 无法使用 Alamofire 从这个 swift 类访问我的 API

iOS:删除 UItableviewCell 时出错:-[UITableView _endCellAnimationsWithContext:] 中的断言失败

ios - 导航项中不支持 Swift 纯样式

ios - UITableView - 具有相同数组的部分中的不同行

objective-c - 过多的 UITableView 单元格

ios - iOS 中的 CloudKit 和 recordType。记录类型允许使用哪些字符?