在滚动后,collectionView 中可重用单元格的子视图大小不正确

4

我有一个用于聊天页面的collectionView。我创建了一个名为TextChatCollectionViewCellUICollectionViewCell子类。

我还有另一个类来填充我的collectionView并为每个项目指定CGSize(遵循UICollectionViewDelegateFlowLayoutUICollectionViewDataSource协议)。

当我滚动时,单元格框架大小是正确的,但子视图的框架大小是错误的,这可能是因为dequeueReusableCell返回另一个单元格的实例,并且子视图没有重新加载。我尝试调用layoutIfNeeded()以强制布局重新绘制子视图,但它没有任何效果。

我的带约束条件的单元格xib文件如下:

Xib file for TextChatCollectionViewCell

我的填充和返回单元格的代码如下:

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)

你是否在任何地方根据内容重置了单元格大小? - Gagan_iOS
我使用了 func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize,但我没有显式地重置大小。 - Dany Sousa
尝试使用 layoutSubviews - Annie Gupta
很遗憾,layoutSubviews并没有解决这个问题。 - Dany Sousa
1个回答

0

可能是约束在你给单元格赋新值之前没有得到更新。

这里有一小段代码,我希望它能解决问题。 layoutIfNeeded()

在给单元格赋新值之前,只需在单元格上调用此方法即可。

cell.layoutIfNeeded()

为我解决了很多问题。希望它也能帮到你。


我已经尝试调用cell.layoutIfNeeded(),但不幸的是它并没有解决我的问题,抱歉。 - Dany Sousa

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接