Reusable cell mobility in collectionView is wrong size after scrolling

I have a collectionView for a chat page. I created a subclass UICollectionViewCell

named TextChatCollectionViewCell

. I have another class that populates my collection view and specifies CGSize

for each item (following UICollectionViewDelegateFlowLayout and UICollectionViewDataSource protocols).

The cell size of the cell is correct, but the subviews have the wrong frame size when scrolling, possibly because it dequeueReusableCell

returns an instance of another cell and the subviews does not reload, I tried calling layoutIfNeeded()

to force the layout of the subviews again, but that has no effect.

My xib file for my constrained cell:

Xib file for TextChatCollectionViewCell

My code for filling and returning a cell:

public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    guard let cell = self.manager.collectionView.dequeueReusableCell(withReuseIdentifier: "TextChatCollectionViewCell", for: indexPath) as? TextChatCollectionViewCell else {
        return UICollectionViewCell()
    }

    self.fillCellUsingMessageUUID(cell, self.messagesUUIDs[indexPath.row]) // Hide labelDate (set height to 0 for the moment), fill UITextView etc...


    print("=================\(self.messagesUUIDs[indexPath.row])===================")
    print("Cell text : " + cell.textMessage.text)
    print("Cell frame size : " + cell.frame.size.debugDescription)
    print("UITextView frame size : " + cell.textMessage.frame.size.debugDescription)
    print("UITextView parent size : " + cell.textBackgroundView.frame.size.debugDescription)
    print("Cell content view frame size : " + cell.globalView.frame.size.debugDescription)
    print("====================================")
    return cell
}

      

My debug output:

=================B00C74D6-C3F1-4039-948B-0BAC59DC0D83===================

Cell text : "Ggg"
Cell frame size : (320.0, 80.0) // Expected size
UITextView frame size : (240.0, 59.5) // Wrong height
UITextView parent size : (240.0, 59.5) // Wrong height
Cell content view frame size : (320.0, 59.5) // Wrong height
====================================
=================2704FFF5-17D1-4E0E-9399-DD7EB4C60D36===================
Cell text : "ZyehdhzhdhdhzhejajeksvshdjajdjhhhjhhjjhhhhthhytgjuhjjyghjuyghhuygghuutghjutghkiygvcwqazdxcfeerggvbhtyjjnkkuilloomppolpƓkkjjƮƮƮukhgkurghhhhhgoohgosohsohdohsohsohshowlhdlhslhslhsglslydlgsotwyod [...]"
Cell frame size : (320.0, 411.5) // Expected size
UITextView frame size : (240.0, 59.5) // Wrong height
UITextView parent size : (240.0, 59.5) // Wrong height
Cell content view frame size : (320.0, 59.5) // Wrong height

      

Expected Result:

=================B00C74D6-C3F1-4039-948B-0BAC59DC0D83===================

Cell text : "Ggg"
Cell frame size : (320.0, 80.0)
UITextView frame size : (240.0, 80.0)
UITextView parent size : (240.0, 80.0)
Cell content view frame size : (320.0, 80.0)
====================================
=================2704FFF5-17D1-4E0E-9399-DD7EB4C60D36===================
Cell text : "ZyehdhzhdhdhzhejajeksvshdjajdjhhhjhhjjhhhhthhytgjuhjjyghjuyghhuygghuutghjutghkiygvcwqazdxcfeerggvbhtyjjnkkuilloomppolpƓkkjjƮƮƮukhgkurghhhhhgoohgosohsohdohsohsohshowlhdlhslhslhsglslydlgsotwyod [...]"
Cell frame size : (320.0, 411.5) 
UITextView frame size : (240.0, 411.5) 
UITextView parent size : (240.0, 411.5)
Cell content view frame size : (320.0, 411.5)

      

+3


source to share


1 answer


The constraint is probably not updated before assigning new values ā€‹ā€‹to the cell.

Here's a little code that I hope will fix it. layoutIfNeeded()

Just call the cell before assigning new values ā€‹ā€‹to it, like



cell.layoutIfNeeded()

I had a lot of problems. Hope this helps you.

0


source







All Articles