将会话页面中文本消息已读 UI 修改为文本“已读”、“未读”

SDK 会话页面中文本消息已读的 UI 默认是一个“对勾”图标,现要修改为文本“已读”或“未读”
发布时间: 2019-11-12 11:03

回答:

解决思路:

1. 在 cell 显示的时候,将 SDK 默认的图标移除,在对应位置添加文本“已读”或“未读”

2. 需要监听消息发送状态更新的通知,更新 cell


解决方案:

1. 重写 cell 即将显示的方法,修改 UI

- (void)willDisplayMessageCell:(RCMessageBaseCell *)cell atIndexPath:(NSIndexPath *)indexPath {
    RCMessageModel *model = [self.conversationDataRepository objectAtIndex:indexPath.row];
    if ([cell isKindOfClass:[RCTextMessageCell class]] && model.conversationType == ConversationType_PRIVATE) {
        if (model.content) {
            for (UIView *view in [((RCMessageCell *)cell).messageHasReadStatusView subviews]) {
                [view removeFromSuperview];
            }
            UILabel *hasReadView = [[UILabel alloc] initWithFrame:CGRectMake(-18, 5, 30, 20)];
            hasReadView.textAlignment = NSTextAlignmentRight;
            hasReadView.font = [UIFont systemFontOfSize:12];
            hasReadView.textColor = [UIColor blackColor];
            ((RCMessageCell *)cell).messageHasReadStatusView.hidden = NO;
            if (model.sentStatus == SentStatus_READ) {
                CGRect hasReadViewFrame = hasReadView.frame;
                hasReadViewFrame.origin.y -= 19;
                hasReadView.frame = hasReadViewFrame;
                hasReadView.text = @"已读";
                [((RCMessageCell *)cell).messageHasReadStatusView addSubview:hasReadView];
            } else if (model.sentStatus == SentStatus_SENT) {
                hasReadView.text = @"未读";
                [((RCMessageCell *)cell).messageHasReadStatusView addSubview:hasReadView];
            }
        }
    } else {
    for (UIView *view in [((RCMessageCell *)cell).messageHasReadStatusView subviews]) {
        [view removeFromSuperview];
        }
    }
}

2. 监听消息发送状态更新的通知,执行刷新 cell 的方法:

[[NSNotificationCenter defaultCenter]     
    addObserver:self     
    selector:@selector(messageCellUpdateSendingStatusEvent:)     
    name:KNotificationMessageBaseCellUpdateSendingStatus     
    object:nil];
- (void)messageCellUpdateSendingStatusEvent:(NSNotification *)notification {
    RCMessageCellNotificationModel *notifyModel = notification.object;
    if (notifyModel && ![notifyModel.actionName isEqualToString:CONVERSATION_CELL_STATUS_SEND_PROGRESS]) {
        [self reloadMessageCell:notifyModel.messageId];
    }
}
- (void)reloadMessageCell:(long)messageId {
    __weak typeof(self) __weakself = self;
    dispatch_async(dispatch_get_main_queue(), ^{
        for (int i = 0; i < __weakself.conversationDataRepository.count; i++) {
            RCMessageModel *model = (__weakself.conversationDataRepository)[i];
            if (messageId == model.messageId) {
                NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:0];
                [__weakself hideCellReceiptView:indexPath withMessageModel:model];
                break;
            }
        }
    });
}

- (void)hideCellReceiptView:(NSIndexPath *)indexPath withMessageModel:(RCMessageModel *)model {
    UICollectionViewCell *__cell = [self.conversationMessageCollectionView cellForItemAtIndexPath:indexPath];
    [self.conversationMessageCollectionView reloadItemsAtIndexPaths:@[ indexPath ]];
    //如果是空说明被回收了,重新dequeue一个cell,这里用来解决已读人数闪的问题
    if (__cell) {
        if ([__cell isKindOfClass:[RCMessageCell class]]) {
            dispatch_async(dispatch_get_main_queue(), ^{
                ((RCMessageCell *)__cell).receiptCountLabel.hidden = YES;
            });
        }
    }
}

注:

    上面代码已单聊文本消息为例,如需多更多消息类型修改,可以自行添加判断修改 UI。