Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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?