ios 知识点IMselector

iOS环信自定义消息cell(名片)

2018-01-23  本文已影响217人  乂滥好人
记录环信消息cell自定义,方便后续使用。此自定义是直接在环信Demo上进行修改,整体思路还是根据环信官方文档提供来做的,部分有所改动。

2018-01-23 下午2.06.44.png
一、自定义cell,继承至环信【EaseBaseMessageCell】

1、EaseMedicineCell.h内容

#import <EaseUI/EaseUI.h>

@interface EaseMedicineCell : EaseBaseMessageCell

@end

2、EaseMedicineCell.m内容,※注:此类中重写父类的方法不要遗漏,不然报错※

#import "EaseMedicineCell.h"
#define cellHeight 116
#define bubbleViewHeight cellHeight - 15 // 气泡背景图高度

@interface EaseMedicineCell ()
/** 标题 */
@property (nonatomic, strong) UILabel *titleLb;
/** 描述 */
@property (nonatomic, strong) UILabel *describeLb;
/** 图片 */
@property (nonatomic, strong) UIImageView *iconImgView;
/** 分割线 */
@property (nonatomic, strong) UIView *lienView;
/** 底部 图片+文字 but */
@property (nonatomic, strong) UIButton *bottomView;
@end

@implementation EaseMedicineCell

#pragma mark - 系统回调
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier model:(id<IMessageModel>)model
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier model:model];
    if (self) {
        // 添加子控件
        [self setupSubViewWithModel:model];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return self;
}

#pragma mark - 重写父类方法
/** 获取cell的reuseIdentifier */
+ (NSString *)cellIdentifierWithModel:(id<IMessageModel>)model
{
    return @"EaseMedicineCell";
}

/** 获取cell高度 */
+ (CGFloat)cellHeightWithModel:(id<IMessageModel>)model
{
    return cellHeight;
}

/** 判断是否需要自定义气泡 */
- (BOOL)isCustomBubbleView:(id<IMessageModel>)model
{
    return YES;
}

/** 根据消息model变更气泡样式 */
- (void)setCustomModel:(id<IMessageModel>)model
{
    UIImage *image = model.image;
    if (!image) {
        [self.bubbleView.imageView sd_setImageWithURL:[NSURL URLWithString:model.fileURLPath] placeholderImage:[UIImage imageNamed:model.failImageName]];
    } else {
        _bubbleView.imageView.image = image;
    }
    
    if (model.avatarURLPath) {
        [self.avatarView sd_setImageWithURL:[NSURL URLWithString:model.avatarURLPath] placeholderImage:model.avatarImage];
    } else {
        self.avatarView.image = model.avatarImage;
    }
}

/** 根据消息改变气泡样式 */
- (void)setCustomBubbleView:(id)model{
    _bubbleView.imageView.image = [UIImage imageNamed:@"imageDownloadFail"];
}

/** 更新自定义气泡的边距 */
- (void)updateCustomBubbleViewMargin:(UIEdgeInsets)bubbleMargin model:(id<IMessageModel>)mode
{
    _bubbleView.translatesAutoresizingMaskIntoConstraints = YES;
    CGFloat nameLabelHeight = 15;// 昵称label的高度
    if (mode.isSender) {
        _bubbleView.frame =
        CGRectMake([UIScreen mainScreen].bounds.size.width - 273.5, nameLabelHeight, 213, bubbleViewHeight);
    }else{
        _bubbleView.frame = CGRectMake(55, nameLabelHeight, 213, bubbleViewHeight);
    }
}

#pragma mark - 私有方法
/** 添加子控件 */
- (void)setupSubViewWithModel:(id<IMessageModel>)model
{
    NSLog(@"扩展消息 === %@",model.message.ext);
    self.hasRead.hidden = YES;
    [self.bubbleView.backgroundImageView addSubview:self.titleLb];
    [self.bubbleView.backgroundImageView addSubview:self.describeLb];
    [self.bubbleView.backgroundImageView addSubview:self.iconImgView];
    [self.bubbleView.backgroundImageView addSubview:self.lienView];
    [self.bubbleView.backgroundImageView addSubview:self.bottomView];
}

#pragma mark - lazy
- (UILabel *)titleLb
{
    if (!_titleLb) {
        _titleLb = [[UILabel alloc] initWithFrame:CGRectMake(15, 15, 150, 15)];
        _titleLb.text = @"专属运动处方";
        _titleLb.textColor = [UIColor blackColor];
        _titleLb.font = [UIFont systemFontOfSize:13];
    }
    return _titleLb;
}

- (UILabel *)describeLb
{
    if (!_describeLb) {
        _describeLb = [[UILabel alloc] initWithFrame:CGRectMake(15, 15+15+2, 180, 15)];
        _describeLb.text = @"科学运动处方";
        _describeLb.textColor = [UIColor grayColor];
        _describeLb.font = [UIFont systemFontOfSize:11];
    }
    return _describeLb;
}

- (UIImageView *)iconImgView
{
    if (!_iconImgView) {
        _iconImgView = [[UIImageView alloc] initWithFrame:CGRectMake(213 - 40 - 20, 15+15+2, 40, 40)];
        _iconImgView.image = [UIImage imageNamed:@"sports"];
    }
    return _iconImgView;
}

- (UIView *)lienView
{
    if (!_lienView) {
        _lienView = [[UIView alloc] initWithFrame:CGRectMake(2, bubbleViewHeight - 20, 213-11, 0.5)];
        _lienView.backgroundColor = [UIColor grayColor];
    }
    return _lienView;
}

- (UIButton *)bottomView
{
    if (!_bottomView) {
        _bottomView = [UIButton buttonWithType:UIButtonTypeCustom];
        _bottomView.frame = CGRectMake(0, bubbleViewHeight - 20, 90, 20);
        _bottomView.backgroundColor = [UIColor clearColor];
        [_bottomView setTitle:@"运动处方" forState:UIControlStateNormal];
        [_bottomView setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        _bottomView.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 0, -10);
        [_bottomView setImage:[UIImage imageNamed:@"sports_icon"] forState:UIControlStateNormal];
        _bottomView.titleLabel.font = [UIFont systemFontOfSize:11];
    }
    return _bottomView;
}

@end

二、控制器判断是否显示自定义消息cell

注:※我这是直接在红包控制器(RedPacketChatViewController)进行判断的,项目中可以自己写一个类继承ChatViewController去实现,原理一样。※

1、修改自定义cell的头像 2018-01-23 下午2.27.23.png
2、在- (UITableViewCell *)messageViewController: (UITableView *)tableView cellForMessageModel (id<IMessageModel>)messageModel 方法中进行判断是否显示自定义消息cell【注:在此的判断在下方 5、中说明】
2018-01-23 下午2.35.23.png 3、在- (CGFloat)messageViewController:(EaseMessageViewController *)viewController heightForMessageModel:(id<IMessageModel>)messageModel withCellWidth:(CGFloat)cellWidth方法中返回自定义消息cell的高度 2018-01-23 下午2.32.46.png

4、在- (void)messageViewController:(EaseMessageViewController *)viewController didSelectMoreView:(EaseChatBarMoreView *)moreView AtIndex:(NSInteger)index方法中找到触发按钮,实现发送自定义消息cell

2018-01-23 下午2.42.29.png

4.1、[self sendPrescription] 方法实现:

2018-01-23 下午2.44.10.png

5、判断说明:在环信的EaseMessageViewController.m的- (void)messageCellSelected:(id<IMessageModel>)model方法中添加一个case。

2018-01-23 下午2.48.29.png

5.1、上面自己添加的case中代理方法

2018-01-23 下午2.51.28.png

6、还是在红包控制器(RedPacketChatViewController)中实现代理(因为直接在红包控制器修改的代码,所以还在这个控制器实现),在代理方法中根据需求做相应操作。

2018-01-23 下午2.53.58.png

至此已经基本实现自定义消息cell功能。后期可以根据自己需求进行优化。最后总结一下大概步骤: ①新建cell --> ②判断cell类型来显示cell及高度 --> ③触发自定义cell,发送显示消息 --> ④实现代理完成操作

上一篇 下一篇

猜你喜欢

热点阅读