Group Group Group Group Group Group Group Group Group

Dynamic cell sizing isn't actually dynamic

#1

Hello every one ,

As I go deeper in customization I keep find errors in my approach of coding and solving problems that is very good , because one won’t learn without mistakes.
At fitst , I read the article by Paride Broggi : UICollectionView Custom Layout Tutorial: Pinterest which is great if you are looking for hard coded implementation for loading data, or that what I experienced during reading and testing the article content . Ok, so I decided to read further and try to understand why when I try the coded in the article and change what is nesesry for my app to work , it doesn’t work will, images width and height looks ugly because they stretch trying to fit the constant dimensions of the item . Something missing ,I reasoned , a single line like setting the UIImage property content mode to .scaleToFit isn’t engouh . So kept looking at the video tutorials, I found this course by @micpringle UICollectionView Custom Layout
Awesome , found what is missing , a couple of height and protocol providers and importing AVFoundation and it will work.

That is not it, I started to face another type of problems , cells content disappear and cell content changes . I tried many solutions, I can tell the the problem is in my custom layout class when the following method is called:
override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {

        var layoutAttributes = [UICollectionViewLayoutAttributes]()
        for attributes in cache {
            if attributes.frame.intersects(rect) {
                layoutAttributes.append(attributes)
                print(layoutAttributes)
            }
        }
        return layoutAttributes
    }

But I wounder why would this happen when based on my understanding this method returns the cached attributes of the item that intersect with the visible rect in the collection view.
For the record , I code progtamatically , but this should not be an issue , and I load data form core data base, which means I also add items to the core data base as will. I also use these methods for inserting and deleting :

var deletedItems: [IndexPath]?
override func finalizeCollectionViewUpdates() {

}
override func finalLayoutAttributesForDisappearingItem(at itemIndexPath: IndexPath) -> UICollectionViewLayoutAttributes? {

guard let attributes = super.finalLayoutAttributesForDisappearingItem(at: itemIndexPath), let deleted = deletedItems, deleted.contains(itemIndexPath) else {
    return nil
}
attributes.alpha = 1.0
attributes.transform = CGAffineTransform(scaleX: 0.1, y: 0.1)
attributes.zIndex = -1
return attributes

}

var addedItem: IndexPath?

override func initialLayoutAttributesForAppearingItem(at itemIndexPath: IndexPath) -> UICollectionViewLayoutAttributes? {

guard let attributes = super.initialLayoutAttributesForAppearingItem(at: itemIndexPath), let added = addedItem, added == itemIndexPath else {
    return nil
}

attributes.center = CGPoint(x: collectionView!.frame.width - 23.5, y: -24.5)
attributes.alpha = 1.0
attributes.transform = CGAffineTransform(scaleX: 0.15, y: 0.15)
attributes.zIndex = 5

return attributes

}

And that’s how I implement UIcollectionView methods :

override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
    {
           return  arrProjects.count
    }
    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
    {
       
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellid, for: indexPath) as! IdeaCell
         cell.instancee = arrProjects[indexPath.item]
         cell.isEditing = isEditing

         return cell
    }  

Problem is:
After I add different sizes of pictures , some these pictures after inserting starts to change in size and replicate pictures with smaller height like the following :
First inserting , no problem :

Second inserting , no problem :

Third inserting , here it happens(Not necessarily each with same order) :

Could someone help me with this.

#2

@ahmedsabir Do you still have issues with this?

#3

Yes, I do and I stoped for a while with this project.

#4

Hi @ahmedsabir,
I have not run the code but when you are dealing with transforms, I have a feeling from the description of the problem that you are modifying the attributes and the next time using the last ones as base for the next transform - hence the interesting results.

cheers,