My collection view cells are being modified when scrolling all the way to the bottom and then back up.
In my viewDidLoad I have a Parse query which fetches all the values from the database and then calls reloadData from the main thread.
On the initial load the viewable cells are correctly displayed.
But when I scroll down the cells that were not visible are loaded and 1/3 of them are displayed incorrectly.
Then when I scroll back up to the initial viewable cells the first one is not displayed properly.
By not displayed properly I mean in my database is a field for alignment. This can hold center, left, or right Strings.
This value is how my image and buttons should be aligned in the cell.
Here is my cellForRow function.
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell{
let cell: FeedThumbnail = collectionView.dequeueReusableCellWithReuseIdentifier("feed_cell", forIndexPath: indexPath) as! FeedThumbnail
//handle cell alignment
if(align[indexPath.row] == "left"){
//leave alignment as is for buttons
if(cell.picture.frame.origin.x == 0){
cell.picture.frame.offsetInPlace(dx: -75, dy: 0)
}
}
else if(align[indexPath.row] == "center"){
cell.holder_view.frame.offsetInPlace(dx: 105 - cell.holder_view.frame.origin.x, dy: 0)
//leave image as is
}
else if(align[indexPath.row] == "right"){
cell.holder_view.frame.offsetInPlace(dx: 220 - cell.holder_view.frame.origin.x, dy: 0)
cell.picture.frame.offsetInPlace(dx: 100 - cell.picture.frame.origin.x, dy: 0)
}
return cell
}
Here is the class for my collection view cell
import UIKit
class FeedThumbnail: UICollectionViewCell {
@IBOutlet weak var picture: UIImageView!
@IBOutlet weak var comment_btn: UIButton!
@IBOutlet weak var heart_btn: UIButton!
@IBOutlet weak var rescip_btn: UIButton!
@IBOutlet weak var og_btn: UIButton!
@IBOutlet weak var name_lbl: UIButton!
@IBOutlet weak var holder_view: UIView!
}
FeedThumbnail
class?prepareForReuse
in yourFeedThumbnail
class and reset all of your frames back to default.