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