【问题标题】:Data has been replaced or missing while scrolling collectionview滚动集合视图时数据已被替换或丢失
【发布时间】:2019-11-07 07:25:01
【问题描述】:

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

我在这里添加屏幕截图,以显示滚动前和滚动后得到的输出,请查看

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方法

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

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

【问题讨论】:

    标签: ios swift uitableview uicollectionview uiscrollview


    【解决方案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
    }
    

    文本颜色为白色,因此与它的 superview 的背景相同,因此不会出现

    【讨论】:

    • 谢谢我刚刚犯了愚蠢的错误我解决了我的问题
    • 真棒答案:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-08-12
    • 2016-02-08
    • 1970-01-01
    • 1970-01-01
    • 2014-12-28
    • 2019-02-21
    • 2016-06-24
    相关资源
    最近更新 更多