UITableViewCell ImageView无法正确缩放? [英] UITableViewCell ImageView not scaling properly?
问题描述
我正在用用户选择的图像填充UITableView
.我希望表格单元格中的缩略图大小均相同,而不会影响宽高比,以免拉伸/歪斜图像,这听起来像ScaleAspectFill
,但是UIViewContentMode
的选择似乎都没有产生效果在那里,我出现了一些相互矛盾的方法,尤其是heightForRowAtIndexPath
,但是删除这些会使我的单元格变得太小.以下是我的didSelectRowAtIndexPath
和heightForRowAtIndexPath
方法,以及我当前代码的模拟器的屏幕截图(使用模拟器库存图片).任何帮助表示赞赏.
I am populating a UITableView
with images selected by the user. I'd like to the have thumbnails in the table cells all be the same size without affecting the aspect ratio so as not to stretch/skew the images, which sounds to me like ScaleAspectFill
, however none of the UIViewContentMode
selections seem to have an effect. There me conflicting methods, notable heightForRowAtIndexPath
, but removing this makes my cells too small. The following are my didSelectRowAtIndexPath
and heightForRowAtIndexPath
methods, along with a screen shot from the simulator of my current code (using simulator stock images). Any help is appreciated.
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
方法:
You can add a subclass of UITableViewCell, then overrides layoutSubviews
method:
override func layoutSubviews() {
super.layoutSubviews()
self.imageView.frame = CGRect(0,0,200,100)
}
这篇关于UITableViewCell ImageView无法正确缩放?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!