gpt4 book ai didi

ios - 滚动 collectionview 时数据已被替换或丢失

转载 作者:行者123 更新时间:2023-11-28 11:30:50 26 4
gpt4 key购买 nike

我正在将 Collection View 作为我的 ChatMessagViewController 来实现,并且我在 Collection View 中填充消息所有事情对我来说都很完美,但问题是当我滚动 Collection View 消息时。在滚动条上发出或替换让我向您展示我用于填充 collectionview 的代码

我在这里添加了滚动前和滚动后得到的输出的屏幕截图,请看一下

func loadMessageData(){
self.message.removeAll()
guard let uid = Auth.auth().currentUser?.uid else{
return
}
let userref = Database.database().reference().child("Message").child(uid)
userref.child(self.senderID!).observe(.childAdded, with: { (snapshot) in
print(snapshot)
if let dictonary = snapshot.value as? [String:AnyObject]{


let key = snapshot.key
let mainDict = NSMutableDictionary()
mainDict.setObject(key, forKey: "userid" as NSCopying)
self.namesArray.add(mainDict)


let message = Message(dictionary: dictonary)
self.message.append(message)

self.timer?.invalidate()
self.timer = Timer.scheduledTimer(timeInterval: 0.1, target: self, selector: #selector(self.handleReload), userInfo: nil, repeats: false)

}
}, withCancel: nil)
}




extension ChatViewController: UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout{

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return message.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellID, for: indexPath) as! ChatMessageCell
let message1 = message[indexPath.item]
cell.tetxtView.text = message1.msg
cell.bubbleWidthAnchor?.constant = estimatedFrameForText(text: message1.msg!).width + 32
let ketID = message1.key_id

if ketID == Auth.auth().currentUser?.uid{
cell.bubbleView.backgroundColor = UIColor(red: 255, green: 255, blue: 255, alpha: 1)
cell.bubbleViewRightAnchor?.isActive = false
cell.bubbleViewLeftAnchor?.isActive = true
}else{
cell.bubbleView.backgroundColor = UIColor(red: 0/255, green: 158/255, blue: 214/255, alpha: 1)
cell.tetxtView.textColor = UIColor.white
cell.bubbleViewRightAnchor?.isActive = true
cell.bubbleViewLeftAnchor?.isActive = false
}
return cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
var height: CGFloat = 80

if let mesg = message[indexPath.item].msg{
height = estimatedFrameForText(text: mesg).height + 20
}
return CGSize(width: view.frame.width, height: height)
}

}

这是用于填充的 collectionview 方法

问题是当我滚动时消息被替换或丢失

请先检查屏幕截图我收到消息,滚动后我回到顶部消息丢失

最佳答案

cell.tetxtView.textColor 已出列确保将其添加到 if

if ketID == Auth.auth().currentUser?.uid{
cell.bubbleView.backgroundColor = UIColor(red: 255, green: 255, blue: 255, alpha: 1)
cell.tetxtView.textColor = UIColor.black /////// here
cell.bubbleViewRightAnchor?.isActive = false
cell.bubbleViewLeftAnchor?.isActive = true
}else{
cell.bubbleView.backgroundColor = UIColor(red: 0/255, green: 158/255, blue: 214/255, alpha: 1)
cell.tetxtView.textColor = UIColor.white
cell.bubbleViewRightAnchor?.isActive = true
cell.bubbleViewLeftAnchor?.isActive = false
}

文本颜色为白色,与父 View 的背景相同,因此不显示

关于ios - 滚动 collectionview 时数据已被替换或丢失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56754332/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com