記錄環(huán)信消息cell自定義,方便后續(xù)使用冻记。此自定義是直接在環(huán)信Demo上進行修改,整體思路還是根據(jù)環(huán)信官方文檔提供來做的来惧,部分有所改動冗栗。
一、自定義cell违寞,繼承至環(huán)信【EaseBaseMessageCell】
1贞瞒、EaseMedicineCell.h內(nèi)容
#import <EaseUI/EaseUI.h>
@interface EaseMedicineCell : EaseBaseMessageCell
@end
2、EaseMedicineCell.m內(nèi)容趁曼,※注:此類中重寫父類的方法不要遺漏军浆,不然報錯※
#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 - 系統(tǒng)回調(diào)
- (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;
}
/** 根據(jù)消息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;
}
}
/** 根據(jù)消息改變氣泡樣式 */
- (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 = @"科學(xué)運動處方";
_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去實現(xiàn)乒融,原理一樣∩忝酰※
2、在- (UITableViewCell *)messageViewController: (UITableView *)tableView cellForMessageModel (id<IMessageModel>)messageModel 方法中進行判斷是否顯示自定義消息cell【注:在此的判斷在下方 5奢驯、中說明】
4、在- (void)messageViewController:(EaseMessageViewController *)viewController didSelectMoreView:(EaseChatBarMoreView *)moreView AtIndex:(NSInteger)index方法中找到觸發(fā)按鈕瘪阁,實現(xiàn)發(fā)送自定義消息cell
4.1撒遣、[self sendPrescription] 方法實現(xiàn):
5、判斷說明:在環(huán)信的EaseMessageViewController.m的- (void)messageCellSelected:(id<IMessageModel>)model方法中添加一個case管跺。
5.1义黎、上面自己添加的case中代理方法
6、還是在紅包控制器(RedPacketChatViewController)中實現(xiàn)代理(因為直接在紅包控制器修改的代碼豁跑,所以還在這個控制器實現(xiàn))廉涕,在代理方法中根據(jù)需求做相應(yīng)操作。
至此已經(jīng)基本實現(xiàn)自定義消息cell功能艇拍。后期可以根據(jù)自己需求進行優(yōu)化狐蜕。最后總結(jié)一下大概步驟: ①新建cell --> ②判斷cell類型來顯示cell及高度 --> ③觸發(fā)自定義cell,發(fā)送顯示消息 --> ④實現(xiàn)代理完成操作