Brian Josel Brian Josel - 1 year ago 97
Swift Question

UITableViewCell ImageView not scaling properly?

I am populating a

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
, however none of the
selections seem to have an effect. There me conflicting methods, notable
, but removing this makes my cells too small. The following are my
methods, along with a screen shot from the simulator of my current code (using simulator stock images). Any help is appreciated. 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

Answer Source

You can add a subclass of UITableViewCell, then overrides layoutSubviews method:

override func layoutSubviews() {

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