滚动 UICollectionView 时项目颜色变化

问题描述 投票:0回答:0

这是一个简单的问题,但很难找到答案我已经尝试了太多但对我没有任何用处我正在通过包含

read
unread
消息功能的firebase构建聊天应用程序我有这个bool值来自 firstore
read
所以只要 read 变为 true 图像就会变成蓝色否则它是灰色的,这是我现在拥有的代码这个问题在我的应用程序中太旧了我无法解决它因为我也有许多其他人所以我不只是问昨天我遇到了这个问题,直到现在我无法修复它超过一年

这是标准设置

class MessagesCell: UICollectionViewCell{


   let readImg: UIImageView = {
    let view = UIImageView(image: UIImage(named: "readed")?.withRenderingMode(.alwaysTemplate))
    view.layer.masksToBounds = true
    view.contentMode = .scaleAspectFit
    view.translatesAutoresizingMaskIntoConstraints = false
     return view
   }()

    override init(frame: CGRect) {
        super.init(frame: frame)
        addSubview(readImg)

        readImg.topAnchor.constraint(equalTo: messageTextView.bottomAnchor, constant: 5).isActive = true
        readImg.trailingAnchor.constraint(equalTo: messageTextView.trailingAnchor, constant: -8).isActive = true
        readImg.heightAnchor.constraint(equalToConstant: 11).isActive = true
        readImg.widthAnchor.constraint(equalToConstant: 11).isActive = true
    }

   override func prepareForReuse() {

     readImg.image = UIImage(image: UIImage(named: "readed")?.withRenderingMode(.alwaysTemplate)

    }


}

这是我在做的

cellForItemAt

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

      let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "MessagesCell", for: indexPath) as! MessagesCell

      var Messages = messages[indexPath.item]

      cell.messageTextView.text = Messages.text

      if Messages.isSender.boolValue == false{

        // There is some code here not related 

      }else{

      if Messages.read == true{

           cell.readImg.tintColor = .whatsBlue
                
        }else if Messages.read == false{

            cell.readImg.tintColor = .gray

      }
    }

      if let location = Messages.location, location != GeoPoint(latitude: 0.0, longitude: 0.0){

      if Messages.read == true{

          cell.readImg.tintColor = .whatsBlue 
           
       }else if Messages.read == false{

           cell.readImg.tintColor = .gray

        }
      }

      let uid = Auth.auth().currentUser?.uid
        let fireStore = Firestore.firestore()
        let doc = fireStore.collection("inProcess").whereField("Uid", isEqualTo: uid ?? "")
        doc.getDocuments { query, err in
            if err != nil {}
            query?.documents.forEach({ snap in
                let data = snap.reference
                let chatRef = data.collection("chat").order(by: "date")
                ChatListener.shared.driverReadListener = chatRef.addSnapshotListener { snap, err in
                    if err != nil {}
                    snap?.documentChanges.forEach({ change in
                        if change.type == .modified{
                            Messages.read = true
                            cell.readImg.tintColor = .whatsBlue
                        }
                    })
                }
        })
        }

}

我对每个案例都重复相同的 if 语句,所以如果发件人不是我,图片是来自我还是来自其他人等,我认为这不是正确的方法,但这是最多的对我来说可以理解的解决方案我看到有些人提到

prepareForReuse
但是如果有人有更好的主意请我感谢它并没有太大帮助。

这里是一个图像的例子,它的颜色在滚动 UICollectionView 时发生变化

在这张照片中,图像已经被传递,并且读取 bool 值是真的所以图像变成了蓝色并且一切正常

当我滚动 UICollectionView 时会发生这种情况

ios swift uicollectionview uiimageview uicollectionviewcell
© www.soinside.com 2019 - 2024. All rights reserved.