简体   繁体   English

滚动后,collectionView中可重用单元的子视图大小错误

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

I have a collectionView for a chat page. 我有一个用于聊天页面的collectionView。 I created an UICollectionViewCell subclass named TextChatCollectionViewCell . 我创建了一个名为TextChatCollectionViewCellUICollectionViewCell子类。 I have another class that fills my collection view and specify a CGSize for each item (following UICollectionViewDelegateFlowLayout and UICollectionViewDataSource protocols). 我有另一个类填充我的集合视图并为每个项指定一个CGSize (遵循UICollectionViewDelegateFlowLayout和UICollectionViewDataSource协议)。

The cell frame size is correct but subviews have a wrong frame size when I scroll, probably because dequeueReusableCell return an instance of another cell and subviews doesn't reload, I tried to call layoutIfNeeded() to force the layout of subviews drawing again but it has no effect. 我滚动时单元格框架大小正确但是子视图的框架大小错误,可能是因为dequeueReusableCell返回另一个单元格的实例并且子视图没有重新加载,我试图调用layoutIfNeeded()来强制再次绘制子视图的布局但是它没有效果。

My xib file for my cell with constraints : 我的单元格的xib文件有约束:

TextChatCollectionViewCell的Xib文件

My code to fill and return 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 output : 预期产量:

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

It's probably the constraint's are not getting updated before you assign new values to the cell. 在为单元格分配新值之前,可能没有更新约束。

Here is a small code which I hope should fix it. 这是一个小代码,我希望能解决它。 layoutIfNeeded()

Just call this on the Cell before you assign new values to it, like 只需在为其分配新值之前在Cell上调用它,例如

cell.layoutIfNeeded()

Did fix a lot of issues for me. 为我解决了很多问题。 Hope it helps you. 希望它能帮到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么 CollectionView cell insider 其他 collectionView 的大小错误? - Why CollectionView cell insider other collectionView has wrong size? UICollectionView Cell子视图在collectionView:cellForItemAtIndexPath完成后调整大小 - UICollectionView Cell subview resizes after collectionView: cellForItemAtIndexPath is complete 在collectionview上滚动后,dequeueReusableCellWithReuseIdentifier出错 - dequeueReusableCellWithReuseIdentifier gone wrong after scroll on collectionview 如果滚动,swift collectionview单元格didSelectItemAtIndexPath显示错误的单元格 - swift collectionview cell didSelectItemAtIndexPath shows wrong cell if you scroll Swift Collection查看不可重用的单元格 - Swift collectionView non reusable cell 标头顶部的CollectionView单元子视图 - CollectionView Cell subview on top of header 将子视图添加到collectionview单元格jsqmessagesviewcontroller - add subview to collectionview cell jsqmessagesviewcontroller 滚动后 CollectionView 单元格更改大小 - AutoLayout - CollectionView Cell changing size after scrolling - AutoLayout 当内容大小大于单元格大小时,允许collectionview滚动 - allowing collectionview to scroll while the content size is bigger than the cell size 在UITableView中不使用可重用单元格,在每个单元格中使用CollectionView - Not using reusable cell in UITableView with CollectionView in each cell
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM