我在聊天应用程序界面上工作,当我向上和向下滚动时,我的图标图像消失了 . 我知道这可能是由于集合视图单元格的出列性质 . 这个post在SO上表明这可以通过缓存图像来解决 . 但是,在我的应用程序中,我的图标图像加载到资产中,而不是从URL下载 .

我的代码到目前为止:

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! ChatBubbleCollectionViewCell
    let item = messages[indexPath.item]

    if item.fromUserUID == fromUserUID {
        cell.bubbleView.backgroundColor = .customPink
        cell.textView.text = item.message
        cell.textView.textColor = .white
        cell.imageTextView.textColor = .white
        cell.bubbleLeftAnchor.isActive = false
        cell.bubbleRightAnchor.isActive = true

        if let _ = item.imageUrl {
            cell.imageIconView.image = UIImage(named: "ic_photo_white")
        }
    } else {
        cell.bubbleView.backgroundColor = .faintGray
        cell.textView.text = item.message
        cell.textView.textColor = .black
        cell.imageTextView.textColor = .darkGray
        cell.bubbleLeftAnchor.isActive = true
        cell.bubbleRightAnchor.isActive = false

        if let _ = item.imageUrl {
            cell.imageIconView.image = UIImage(named: "ic_photo_pink")
        }
    }

    let longPressGesture = UILongPressGestureRecognizer(target: self, action: #selector(longPressOnImage))
    longPressGesture.minimumPressDuration = 1

    if let message = item.message {
        cell.bubbleWidthAnchor.isActive = false
        cell.bubbleWidthAnchor.constant = estimatedFrame(for: message).width + 11
        cell.bubbleWidthAnchor.isActive = true
        cell.bubbleHeightAnchor.isActive = false
        cell.imageContainerView.isHidden = true
        cell.textView.isHidden = false
        cell.messageImageView.isHidden = true
        cell.removeGestureRecognizer(longPressGesture)
    } else {
        cell.bubbleWidthAnchor.isActive = false
        cell.bubbleWidthAnchor.constant = 120
        cell.bubbleWidthAnchor.isActive = true
        cell.bubbleHeightAnchor.isActive = true
        cell.messageImageView.isHidden = true
        cell.imageTextView.text = "Fetching..."

        if let imageReadReceipt = item.readReceipt {
            if imageReadReceipt {
                DispatchQueue.main.async {
                    cell.imageTextView.text = "Deleted"
                    cell.imageIconView.image = UIImage(named: "ic_visibility_off")
                    cell.isUserInteractionEnabled = false
                }
            } else {
                if let imageUrlString = item.imageUrl {
                    cell.messageImageView.sd_setImage(with: URL(string: imageUrlString), completed: { (image, error, cache, url) in
                        if let error = error {
                            print(error.localizedDescription)
                        } else if let _ = image {
                            DispatchQueue.main.async {
                                cell.isUserInteractionEnabled = true
                                cell.imageContainerView.isHidden = false
                                cell.imageTextView.text = "Tap and hold to view"
                                cell.textView.isHidden = true
                                cell.imageContainerView.addGestureRecognizer(longPressGesture)
                            }
                        }
                    })
                }
            }
        }
    }

    return cell
}

cellForRow需要做很多事情,我怀疑这是图标消失的原因 . 它必须识别是否接收或发送消息(以便可以在左侧或右侧绘制气泡),或者消息是图像消息还是文本消息 .

上下各种卷轴后,结果如下:

在滚动之前
enter image description here

滚动几次后
enter image description here

可以观察到deleteIcon图像已经删除了“已删除”文本 . 有人会帮忙指出我哪里出错了吗?谢谢 .