簡體   English   中英

滾動后,collectionView中可重用單元的子視圖大小錯誤

[英]Subview of reusable cell in collectionView have wrong size after scroll

我有一個用於聊天頁面的collectionView。 我創建了一個名為TextChatCollectionViewCellUICollectionViewCell子類。 我有另一個類填充我的集合視圖並為每個項指定一個CGSize (遵循UICollectionViewDelegateFlowLayout和UICollectionViewDataSource協議)。

我滾動時單元格框架大小正確但是子視圖的框架大小錯誤,可能是因為dequeueReusableCell返回另一個單元格的實例並且子視圖沒有重新加載,我試圖調用layoutIfNeeded()來強制再次繪制子視圖的布局但是它沒有效果。

我的單元格的xib文件有約束:

TextChatCollectionViewCell的Xib文件

我填寫並返回單元格的代碼:

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
}

我的調試輸出:

=================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

預期產量:

=================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)

在為單元格分配新值之前,可能沒有更新約束。

這是一個小代碼,我希望能解決它。 layoutIfNeeded()

只需在為其分配新值之前在Cell上調用它,例如

cell.layoutIfNeeded()

為我解決了很多問題。 希望它能幫到你。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM