騰訊云IM的sdk中主要提供了 以下幾個消息類
TIMTextElem? (文本消息)
TIMImageElem? (圖片消息)
TIMFileElem? (文件消息)
TIMSoundElem (語音消息)
TIMLocationElem? (地理位置)
TIMFaceElem? (表情消息類型)
TIMVideoElem (微視頻消息)
TIMUGCElem? (UGC視頻)
一般來說這些消息類都可以滿足我們的需求,然而產(chǎn)品和boss 才是老大,他們定方案了,苦逼的程序員還得費力去實現(xiàn).
這個時候自定義消息就派的上用場了.我們先來來看看這個自定義的消息類? TIMCustomElem
/**
*? 自定義消息類型
*/
@interface TIMCustomElem : TIMElem
/**
*? 自定義消息二進(jìn)制數(shù)據(jù)
*/
@property(nonatomic,strong) NSData * data;
/**
*? 自定義消息描述信息,做離線Push時文本展示(已廢棄椅您,請使用TIMMessage中offlinePushInfo進(jìn)行配置)
*/
@property(nonatomic,strong) NSString * desc DEPRECATED_ATTRIBUTE;
/**
*? 離線Push時擴展字段信息(已廢棄冗荸,請使用TIMMessage中offlinePushInfo進(jìn)行配置)
*/
@property(nonatomic,strong) NSString * ext DEPRECATED_ATTRIBUTE;
/**
*? 離線Push時聲音字段信息(已廢棄,請使用TIMMessage中offlinePushInfo進(jìn)行配置)
*/
@property(nonatomic,strong) NSString * sound DEPRECATED_ATTRIBUTE;
@end
注釋已經(jīng)很明顯 主要是有個屬性 是NSData 類型的? ,就是我們需要把自定義的消息封裝轉(zhuǎn)化成NSData傳遞過去.
樓主的需求中是需要自定義個消息,展示訂單信息,需要顯示下單者的聯(lián)系方式 ,地址信息 收貨人 ,商品圖片,商品價格,商品名等信息
構(gòu)造過程
NSMutableDictionary* mesDict=[NSMutableDictionary new];
? [mesDict setObject:@"訂單描述" forKey:KTIMCustomGoodsDescp];
? [mesDict setObject:@"南京橙子哈哈哈啊哈哈哈哈哈 啊哈哈哈哈哈哈哈哈" forKey:KTIMCustomGoodsName];
? [mesDict setObject:@"75.00" forKey:KTIMCustomGoodsPrice];
? ? [mesDict setObject:@"101" forKey:KTIMCustomGoodsType];
? ? [mesDict setObject:@"https://imgs.nanniwan.com/upload/nnw/avatar/2017/08/14/1502701925339.jpg" forKey:KTIMCustomGoodsImage];
? [mesDict setObject:@"2" forKey:KTIMCustomGoodsNum];
? ? [mesDict setObject:@"張三" forKey:KTIMCustomGoodsReceiver];
? ? [mesDict setObject:@"13869746765" forKey:KTIMCustomGoodsMobile];
? ? [mesDict setObject:@"湖北省武漢市江夏區(qū)光谷科技港棟9樓1220" forKey:KTIMCustomGoodsAddress];
// 轉(zhuǎn)換為NSData
NSData* data=[NSJSONSerialization dataWithJSONObject:mesDict options:NSJSONWritingPrettyPrinted error:nil];
TIMCustomElem * custom_elem = [[TIMCustomElem alloc] init];
[custom_elem setData:data];
TIMMessage * msg = [[TIMMessage alloc] init];
[msg addElem:custom_elem];
構(gòu)造后的消息體形式為
data={
"mobile" : "13869746765",
"goods_image" : "https:\/\/imgs.nanniwan.com\/upload\/nnw\/avatar\/2017\/08\/14\/1502701925339.jpg",
"address" : "湖北省武漢市江夏區(qū)光谷科技港棟9樓1220",
"receiver" : "張三",
"goods_price" : "75.00",
"type" : "101",
"goods_name" : "南京橙子哈哈哈啊哈哈哈哈哈 啊哈哈哈哈哈哈哈哈",
"goods_descp" : "訂單描述",
"goods_num" : "2"
}, desc=, ext=
//消息構(gòu)成后我們需要把消息發(fā)送出去? 調(diào)用一下這個方法 先獲取會話
/**
*? 獲取會話
*
*? @param type 會話類型掂为,TIM_C2C 表示單聊 TIM_GROUP 表示群聊
*? ? ? ? ? ? ? TIM_SYSTEM 表示系統(tǒng)會話
*? @param receiver C2C 為對方用戶 identifier,GROUP 為群組Id,SYSTEM為@""
*
*? @return 會話對象
*/
- (TIMConversation*)getConversation:(TIMConversationType)type receiver:(NSString*)receiver;
TIMConversation * conversation = [[TIMManager sharedInstance] getConversation:TIM_C2C receiver:receiver];
//摻入消息
[conversation sendMessage:msg succ:^(){
NSLog(@"消息插入成功");
}
}fail:^(int code, NSString * err) {
NSLog(@"消息插入失敗");
}];
這樣自定義的消息插入就成功了,但是要把自定義的消息正確顯示在界面上還要自定義一個消息顯示的cell類
我們在 ChatTableViewCell增加一個類ChatCustomOrderTableViewCell 讓它繼承于ChatBaseTableViewCell
@interface ChatCustomOrderTableViewCell : ChatBaseTableViewCell
{
@protected
UILabel? ? *_descLabel;//商品描述
UILabel? ? *_goodsNameLabel;//商品名
UILabel? ? *_goodsPriceLabel;//商品價格
UILabel? ? *_goodsNumLabel;//商品數(shù)量
UIImageView? ? *_goodsImage;//商品圖片
UILabel? ? *_seperateLabel;//分割線
UILabel? ? *_contactNameLabel;//聯(lián)系人
UILabel? ? *_phoneLabel;//電話號碼
UILabel? ? *_addressLabel;//地址信息
}
@end
//然后實現(xiàn)該這個類
//商品名
#define KTIMCustomGoodsName @"goods_name"
//圖片名
#define KTIMCustomGoodsImage @"goods_image"
//價格名
#define KTIMCustomGoodsPrice @"goods_price"
//描述
#define KTIMCustomGoodsDescp @"goods_drsp"
//類型
#define KTIMCustomGoodsType @"type"
////自定訂單義消息字段
//數(shù)量
#define KTIMCustomGoodsNum @"goods_num"
//聯(lián)系人
#define KTIMCustomGoodsReceiver @"receiver"
//聯(lián)系人電話
#define KTIMCustomGoodsMobile @"mobile"
//聯(lián)系人電話
#define KTIMCustomGoodsAddress @"address"
@implementation? ChatCustomOrderTableViewCell
// 只創(chuàng)建,外部統(tǒng)一添加
- (UIView *)addElemContent
{
_elemContentRef=[[UIView alloc]init];
_descLabel=[[UILabel alloc]init];
_descLabel.font=FONT(14);
_descLabel.textColor=COLOR_HEX(@"#333333");
_descLabel.textAlignment= NSTextAlignmentLeft ;
[_elemContentRef addSubview:_descLabel];
_goodsNameLabel=[[UILabel alloc]init];
_goodsNameLabel.font=FONT(13);
_goodsNameLabel.numberOfLines=2;
_goodsNameLabel.textColor=COLOR_HEX(@"#333333");
_goodsNameLabel.textAlignment= NSTextAlignmentLeft ;
[_elemContentRef addSubview:_goodsNameLabel];
_goodsPriceLabel=[[UILabel alloc]init];
_goodsPriceLabel.font=FONT(12);
_goodsPriceLabel.textColor=COLOR_HEX(@"#eb6100");
_goodsPriceLabel.textAlignment= NSTextAlignmentLeft ;
[_elemContentRef addSubview:_goodsPriceLabel];
_goodsImage=[[UIImageView alloc]init];
[_elemContentRef addSubview:_goodsImage];
_seperateLabel=[[UILabel alloc]init];
_seperateLabel.backgroundColor=COLOR_HEX(@"#efefef");
[_elemContentRef addSubview:_seperateLabel];
_contactNameLabel=[[UILabel alloc]init];
_contactNameLabel.font=FONT(12);
_contactNameLabel.textColor=COLOR_HEX(@"#333333");
_contactNameLabel.textAlignment= NSTextAlignmentLeft ;
[_elemContentRef addSubview:_contactNameLabel];
_phoneLabel=[[UILabel alloc]init];
_phoneLabel.font=FONT(12);
_phoneLabel.textColor=COLOR_HEX(@"#333333");
_phoneLabel.textAlignment= NSTextAlignmentLeft ;
[_elemContentRef addSubview:_phoneLabel];
_addressLabel=[[UILabel alloc]init];
_addressLabel.font=FONT(12);
_addressLabel.textColor=COLOR_HEX(@"#333333");
_addressLabel.numberOfLines=2;
_addressLabel.textAlignment= NSTextAlignmentLeft ;
[_elemContentRef addSubview:_addressLabel];
//添加約束
[self keepDistanse];
[self configContent];
return _elemContentRef;
}
//設(shè)置約束
-(void)keepDistanse
{
//mark 全部要已_elemContentRef為基準(zhǔn) 不然不正確
//設(shè)置約束
CGFloat imgW=60;
CGFloat imgH=60;
[_descLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(_elemContentRef.top).offset(6);
make.left.mas_equalTo(_elemContentRef.left).offset(15);
make.right.mas_equalTo(_elemContentRef.right).offset(-15);
make.height.mas_equalTo(@16);
}];
[_goodsImage mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(_elemContentRef.left).offset(15);
make.top.mas_equalTo(_elemContentRef.top).offset(6+6+16);
make.width.mas_equalTo(imgW);
make.height.mas_equalTo(imgH);
}];
[_goodsNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(_elemContentRef.top).offset(6+6+16);
make.left.mas_equalTo(_elemContentRef.left).offset(8+imgW+15);
make.right.mas_equalTo(_elemContentRef.right).offset(-15);
make.height.lessThanOrEqualTo(@36);
}];
[_goodsPriceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(_elemContentRef.top).offset(6+6+16+imgH-16);
make.left.mas_equalTo(_elemContentRef.left).offset(8+imgW+15);
make.height.mas_equalTo(@15);
make.right.mas_equalTo(_elemContentRef.right).offset(-15);
}];
[_seperateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(_elemContentRef.top).offset(6+6+16+imgH+8);
make.left.mas_equalTo(_elemContentRef.left).offset(+15);
make.height.mas_equalTo(@0.5);
make.right.mas_equalTo(_elemContentRef.right).offset(-15);
}];
[_contactNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(_elemContentRef.top).offset(6+6+16+imgH+8+8.5);
make.left.mas_equalTo(_elemContentRef.left).offset(+15);
make.height.mas_equalTo(@14);
make.right.mas_equalTo(_elemContentRef.right).offset(-12);
}];
[_phoneLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(_elemContentRef.top).offset(6+6+16+imgH+8+8.5+14);
make.left.mas_equalTo(_elemContentRef.left).offset(+15);
make.height.mas_equalTo(@14);
make.right.mas_equalTo(_elemContentRef.right).offset(-12);
}];
[_addressLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(_elemContentRef.top).offset(6+6+16+imgH+8+8.5+14+14);
make.left.mas_equalTo(_elemContentRef.left).offset(+15);
make.height.lessThanOrEqualTo(@32);
make.right.mas_equalTo(_elemContentRef.right).offset(-12);
}];
}
- (void)configContent
{
[super configContent];
TIMMessage* message=_msg.msg;
TIMElem* item=[message getElem:0];
if([item isKindOfClass:[TIMCustomElem class]]){
TIMCustomElem* elem=(TIMCustomElem*)item;
NSData* data=elem.data;
NSDictionary* dict= [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
//給各個控件賦值
//_descLabel.text=dict[KTIMCustomGoodsDescp];
_descLabel.text=@"訂單描述";
[_goodsImage sd_setImageWithURL:[NSURL URLWithString:dict[KTIMCustomGoodsImage] ] placeholderImage:PLACEHOLDER];
_goodsNameLabel.text=dict[KTIMCustomGoodsName];
NSString* string=[NSString stringWithFormat:@"共%@件商品 ¥%@",dict[KTIMCustomGoodsNum],dict[KTIMCustomGoodsPrice]];
NSString* price=[NSString stringWithFormat:@"¥%@",dict[KTIMCustomGoodsPrice]];
NSArray* attary=@[[ConfigAttributedString foregroundColor:COLOR_HEX(@"#333333") range:NSMakeRange(0, string.length-price.length)],[ConfigAttributedString foregroundColor:COLOR_HEX(@"#eb6100") range:NSMakeRange(string.length-price.length, price.length)]];
_goodsPriceLabel.attributedText=[string createAttributedStringAndConfig:attary];
//_goodsPriceLabel.text=[NSString stringWithFormat:@"共%@件商品 ¥%@",dict[KTIMCustomGoodsNum],dict[KTIMCustomGoodsPrice]];
_contactNameLabel.text=[NSString stringWithFormat:@"聯(lián)系人: %@",dict[KTIMCustomGoodsReceiver]];
// _addressLabel.text=dict[KTIMCustomGoodsAddress];
_addressLabel.text=[NSString stringWithFormat:@"電話: %@",dict[KTIMCustomGoodsAddress]];
_phoneLabel.text=[NSString stringWithFormat:@"地址: %@",dict[KTIMCustomGoodsMobile]];
}
}
//遇到的坑是之前設(shè)置約束時使用相對于兄弟控件來布局一直不正確脊框,后來采用這種死板的布局才達(dá)到要求。践啄。浇雹。