ios - UITableViewCell ImageView 缩放不正确?

标签 ios uitableview swift ios8

我正在用用户选择的图像填充 UITableView。我希望表格单元格中的缩略图都具有相同的大小而不影响纵横比,以免拉伸(stretch)/倾斜图像,这对我来说听起来像 ScaleAspectFill,但是没有一个UIViewContentMode 选择似乎有效果。我的方法存在冲突,值得注意的是 heightForRowAtIndexPath,但删除它会使我的单元格太小。以下是我的 didSelectRowAtIndexPathheightForRowAtIndexPath 方法,以及我当前代码的模拟器屏幕截图(使用模拟器库存图像)。任何帮助表示赞赏。 screenshot from iOS simulator

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    //sets cell based on meme in array
    let cell = tableView.dequeueReusableCellWithIdentifier("memeCell") as! UITableViewCell
    let meme = self.memes[indexPath.row]

    // Set the name and image
    cell.textLabel?.text = meme.topText + " " + meme.bottomText
    cell.imageView?.frame = CGRectMake(0, 0, 200, 100)
    cell.imageView?.contentMode = UIViewContentMode.ScaleToFill //ScaleAspectFill is best, ScaleToFill used to exagerate that its not working
    //cell.imageView?.backgroundColor = UIColor.grayColor() //legacy code, will be removed at commit
    cell.imageView?.image = meme.origImage

    return cell
}

override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    //cell row won't be large enough unless this method is called
    return 75.0
}

最佳答案

您可以添加 UITableViewCell 的子类,然后重写 layoutSubviews 方法:

override func layoutSubviews() {
    super.layoutSubviews()

    self.imageView.frame = CGRect(0,0,200,100)
}

关于ios - UITableViewCell ImageView 缩放不正确?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30424799/

相关文章:

javascript - 如何在 UIWebView 中打开即时弹出窗口?

iphone - 以编程方式为单个 UITableViewCell 设置高度?

swift - 是否可以像 SKScene 一样在 SCNScene 或 SCNNode 中使用 "update(_ currentTime: TimeInterval)"?

Swift - Required Initializer 与 Delegating initializer 之间的一点混淆

swift - 有没有一种方法可以在 Swift 中初始化数组以具有某些类型 T 的重复值

ios - 同一个 UIView 中的多个 UIPickerView

objective-c - 通过系统或其他应用程序使用自己的应用程序打开图像/PDF

ios - 核心数据提取属性 - 基础

ios - 我将如何为我的 UITableView 设计一个异步图像下载器,它根据 TableView 中的位置确定下载的优先级?

ios - 在 TableView 中处理大型 JSON 文件