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

时间:2019-06-25 12:41:13

标签: ios swift uitableview uicollectionview uiscrollview

我正在以我的ChatMessagViewController实现集合视图,并且在collectionview中填充消息,所有事情对我来说都很完美,但是问题是当我滚动集合视图消息时是。滚动时发出问题或替换,让我向您展示用于填充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方法

问题是,当我滚动查看邮件时,邮件已被替换或丢失

enter image description here

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

1 个答案:

答案 0 :(得分:1)

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
}

文本颜色为白色,因此与父视图的背景相同,因此不会出现