cell image is resized when loading TableView

0

I have the following detail.

I have a series of TableViews, in whose cells I am loading background images, so that:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {



    let cell = tableView.dequeueReusableCellWithIdentifier("Cell") as! MenuSecundarioTVCell
    let seccion = items[indexPath.row]


        cell.titulo?.text = (seccion.value["nombre"] as! String)
        //PONIENDO IMAGEN DE FONDO A CELDA
        let imageBackground = UIImageView(frame: CGRectMake(0, 0, cell.frame.width , cell.frame.height+1))
        if let imageRemote = seccion.value["imageBackground"]{
            imageRemoteURL = NSURL(string: imageRemote as! String)
            imageBackground.sd_setImageWithURL(imageRemoteURL!)
            cell.backgroundView = UIView()
            cell.backgroundView!.addSubview(imageBackground)
        }
        //TERMINANDO DE PONER IMAGEN DE FONDO A CELDA

    return cell
}

I am using SDWebImage . The problem is that when the images are loaded into the cells they look like this:

After performing another action such as sliding the cells down or selecting the cell to go to the next view, upon returning the image is rearranged and left in the correct way:

the images are test and it is clear that the resolution of the images does not match the height of the cells (120)

 override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        return 120
    }

Will the origin of the problem be that the resolution of the images must match the size of the cells? I have seen this problem in some apps published in the appstore but the rearrangement is so fast that it is almost imperceptible.

    
asked by Víctor Gonzal 10.04.2016 в 06:58
source

1 answer

1

Add the contentMode the UIImageView just when you create it so that it looks right from the beginning. For example:

let imageBackground = UIImageView(frame: CGRectMake(0, 0, cell.frame.width , cell.frame.height+1))
imageBackground.contentMode = UIViewContentMode.ScaleAspectFit
    
answered by 10.04.2016 / 15:14
source