1、融云会话页面,已读消息默认是显示图标“对勾”,有时候需求让显示成“已读”和“未读”,如何实现自定义呢?下面我们拿文本消息来举例。
2、解决思路:
1)在 cell 显示的时候,将 SDK 默认的图标移除,在对应位置添加文本“已读”或“未读”;
- 需要监听消息发送状态更新的通知,更新 cell。
/*!
即将显示消息Cell的回调
@param cell 消息Cell
@param indexPath 该Cell对应的消息Cell数据模型在数据源中的索引值
@discussion 您可以在此回调中修改Cell的显示和某些属性。
*/
- (void)willDisplayMessageCell:(RCMessageBaseCell *)cell atIndexPath:(NSIndexPath *)indexPath;
3、代码实现
1)文本消息RCTextMessageCell,继承了RCMessageCell,父类中可以看到显示发送状态的view为statusContentView。我们可以移除statusContentView上的其他控件,然后自定义“已读”、“未读”消息的控件,添加在statusContentView上即可。
/*!
显示发送状态的View
@discussion 其中包含messageFailedStatusView子View。
*/
@property (nonatomic, strong) UIView *statusContentView;
/*!
即将显示消息Cell的回调
@param cell 消息Cell
@param indexPath 该Cell对应的消息Cell数据模型在数据源中的索引值
@discussion 您可以在此回调中修改Cell的显示和某些属性。
*/
override func willDisplayMessageCell(_ cell: RCMessageBaseCell!, at indexPath: IndexPath!) {
let model = self.conversationDataRepository.object(at: indexPath.row) as! RCMessageModel
if cell.isKind(of: RCTextMessageCell.self) && (model.conversationType == .ConversationType_PRIVATE) {
if model.content != nil {
for views in (cell as! RCMessageCell).statusContentView.subviews {
views.removeFromSuperview()
}
let statusContentViewFrame: CGRect = (cell as! RCMessageCell).statusContentView.frame
let hasReadView = UILabel.tpLabel(textColor: .OxColorA0A5AB, font: .BoldFont12, textAlignment: .right)
hasReadView.frame = CGRect(x: statusContentViewFrame.size.width-30, y: statusContentViewFrame.size.height-16, width: 30, height: 16)
if model.messageDirection == .MessageDirection_SEND {
(cell as! RCMessageCell).statusContentView.isHidden = false
} else {
(cell as! RCMessageCell).statusContentView.isHidden = true
}
if model.sentStatus == .SentStatus_READ {
// hasReadView.textColor = .OxColorA0A5AB
hasReadView.text = "已读"
(cell as! RCMessageCell).statusContentView.addSubview(hasReadView)
} else if model.sentStatus == .SentStatus_SENT {
// hasReadView.textColor = .OxColorA0A5AB
hasReadView.text = "未读"
(cell as! RCMessageCell).statusContentView.addSubview(hasReadView)
}
}
} else {
if cell.isKind(of: RCMessageCell.self) {
for views in (cell as! RCMessageCell).statusContentView.subviews {
views.removeFromSuperview()
}
}
}
}
2)接收消息发送状态更新的通知KNotificationMessageBaseCellUpdateSendingStatus,刷新cell。在viewDidLoad里面接收通知。
NotificationCenter.default.addObserver(self, selector: #selector(messageCellUpdateSendingStatusEvent(notification:)), name: NSNotification.Name(KNotificationMessageBaseCellUpdateSendingStatus), object: nil)
@objc func messageCellUpdateSendingStatusEvent(notification: Notification) {
guard let notifyModel = notification.object as? RCMessageCellNotificationModel else { return }
if !(notifyModel.actionName == CONVERSATION_CELL_STATUS_SEND_PROGRESS) {
reloadMessageCell(messageId: notifyModel.messageId)
}
}
func reloadMessageCell(messageId: Int) {
DispatchQueue.main.async {
let count = self.conversationDataRepository.count
for i in 0..<count {
let model = self.conversationDataRepository[i] as! RCMessageModel
if messageId == model.messageId {
let indexP = IndexPath.init(item: i, section: 0)
self.hideCellReceiptView(indexPath: indexP, withMessageModel: model)
}
}
}
}
func hideCellReceiptView(indexPath: IndexPath, withMessageModel: RCMessageModel) {
guard let cell = self.conversationMessageCollectionView.cellForItem(at: indexPath) else { return }
self.conversationMessageCollectionView.reloadItems(at: [indexPath])
if cell.isKind(of: RCMessageCell.self) {
DispatchQueue.main.async {
(cell as! RCMessageCell).statusContentView.isHidden = true
}
}
}