ios - 滚动后 collectionView 中可重用单元格的 subview 大小错误

标签 ios swift swift3 uicollectionview uicollectionviewcell

我有一个聊天页面的 collectionView。我创建了一个名为 TextChatCollectionViewCellUICollectionViewCell 子类。 我有另一个类填充我的 Collection View 并为每个项目指定一个 CGSize(遵循 UICollectionViewDelegateFlowLayout 和 UICollectionViewDataSource 协议(protocol))。

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

我的带有约束的单元格的 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)

最佳答案

这可能是在您为单元格分配新值之前约束没有得到更新。

这是一个小代码,我希望它能修复它。 layoutIfNeeded()

在为它分配新值之前,只需在 Cell 上调用它,例如

cell.layoutIfNeeded()

确实为我解决了很多问题。希望对你有帮助。

关于ios - 滚动后 collectionView 中可重用单元格的 subview 大小错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43890675/

相关文章:

具有不同格式的 Swift UITextView

swift - Xcode 9.1 重构不可用

ios - UITextView 左侧图像图标 swift

ios - IF LET 与 GUARD LET 中的返回语句

ios - 如何使用facebook sdk从iphone在facebook上分享截图

ios - UIBarButtonItem 在 iOS 7 中出现问题,这可能是 Apple 的错误吗?

ios - ZBarSymbolSet崩溃

ios - 从 swift 3 发送电子邮件

ios - 如何在选择 Swift 3.0 时缩放 UICollectionViewCell?

iPhone:使用 Facebook 墙 API 发布消息时出现问题