Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Solomon Alexandru
Solomon Alexandru
3,633 Points

Autolayout bug on Collection View when scrolling

Hello! I'm trying to create a feed VC using the collectionView. This is my cellForItemAt, where I'm creating the cell. Note that the cell is looking great, everything aligned with autolayout, before scrolling...then the sad story starts.

   func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! FeedCell

    cell.setNeedsLayout()
    cell.layoutIfNeeded()

    cell.avatarImageView.image = #imageLiteral(resourceName: "avatar-1")
    cell.avatarImageView = makeImageViewRound(image: cell.avatarImageView)
    cell.avatarImageView.contentMode = .scaleAspectFill

    cell.alpha = 1

    if postList.count > 0 {
        cell.alpha = 1
        fetchAvatar(userID: postList[indexPath.row].userID!, completion: { (url) in
            cell.avatarImageView.imageFromServer(urlString: url)
        })
        cell.nameLabel.text = postList[indexPath.row].name
        cell.titleLabel.text = postList[indexPath.row].title
        cell.descriptionLabel.text = postList[indexPath.row].info
        cell.descriptionLabel.sizeToFit()
    }

    return cell
}

And this is my custom cell:

 import Foundation
 import UIKit

 class FeedCell: UICollectionViewCell {
@IBOutlet weak var nameLabel: UILabel!
@IBOutlet weak var titleLabel: UILabel!
@IBOutlet weak var descriptionLabel: UILabel!
@IBOutlet weak var avatarImageView: UIImageView!
@IBOutlet weak var nrLikes: UILabel!
@IBOutlet weak var nrComments: UILabel!
@IBOutlet weak var likeButton: UIImageView!
@IBOutlet weak var commentButton: UIImageView!
@IBOutlet weak var currentDate: UILabel!

override func layoutSubviews() {
    super.layoutSubviews()
    self.layoutIfNeeded()

    self.backgroundColor = .white

    self.layer.masksToBounds = false
    self.layer.shadowOpacity = 0.75
    self.layer.shadowRadius = 5.0
    self.layer.shadowOffset = CGSize.zero
    self.layer.shadowColor = UIColor.gray.cgColor
    self.layer.shadowPath = UIBezierPath(roundedRect: self.bounds, cornerRadius: 1).cgPath

}

}

Maybe I wasn't very specific in words, here is a gif of what happens: http://www.giphy.com/gifs/1rL49MJlIk1P0fsl8h

How can I fix this? Is this an autolayout issue or a collectionview problem?