简体   繁体   中英

Custom UIImageView realigning on touch of UITableViewCell

I have seen this issue a lot of places but have yet to come across a solution that works for me. I have a custom UITableViewCell , in which I have placed a UIImageView . The Image view is supposed to hug the right side of the cell (with constraints from an xib file). Here is the code for how the cell is created and then formatted:

class PlaylistCell: UITableViewCell {


@IBOutlet var imView: UIImageView?
@IBOutlet var label: UILabel?

var playlist:SPTPartialPlaylist? {
    didSet {
        self.configure()
    }
}

func configure()
{
    self.imView?.clipsToBounds = true
    self.label?.text = self.playlist?.name
    let uri = (self.playlist?.images[0] as! SPTImage).imageURL
    dispatch_async(dispatch_get_main_queue(), {
        let data = NSData(contentsOfURL: uri!)
        if (data != nil) {
            self.imView?.image = UIImage(data: data!)
            self.layoutSubviews()
        }
    })
}

And in my ViewController that has the table view in it:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell 
{
    let cell = tableView.dequeueReusableCellWithIdentifier("PlaylistCell") as! PlaylistCell
    cell.playlist = self.playlists[indexPath.row]
    cell.imView?.image = UIImage(named: "placeholder")
    return cell
}

Everything loads correctly and the cells look fine, however when one of the cells is touched, the image snaps to the left side of the cell and decreases in size. Does anyone know why this might be happening? (PS I have tried using SDWebImage and the same issue ensues)

触摸细胞之前

触摸单元格后

Can you try to do add that in your PlayListCell?

override func didMoveToSuperview() {
     self.layoutIfNeeded()
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM