用户聊天列表界面
用户聊天界面一般为三个部分组成,已与用户聊天过的好友名字,最后一次聊天的时间和会话。下面我们就一步一步开始。
NSArray *conversations;//会话数组
NSArray *messArr;//用于tableview数值赋值数组
EMMessage *message;//每条消息
@property(strong,nonatomic)UITableView *mesTabV;
@property(strong,nonatomic)NSMutableArray *friendArr;//好友列表
@property(strong,nonatomic)NSMutableArray *infoArr;//存储最后一条消息
-(void)viewWillAppear:(BOOL)animated
{
[self getChatList];
[self getInfo];
}
1.获取好友列表
获取用户好友列表,最好要放在viewWillAppear,以便用户数据及时刷新
-(void)getChatList
{
EMError *error = nil;
NSArray *userlist = [[EMClient sharedClient].contactManager getContactsFromServerWithError:&error];
if (!error)
{
NSLog(@"获取成功 -- %@",userlist);
}
//把获取的好友列表用一个可变数组存储
self.friendArr=[NSMutableArray arrayWithArray:userlist];
[self.mesTabV reloadData];
}
2.获取与好友最后一次聊天的会话和时间
根据获取回来的好友列表,遍历循环每个好友,通过获取回来的会话,取得最后一条会话,然后取到最后一条会话的时间和会话,用一个可变的dic存储起来,再存在一个可变arr中以便复制。
-(void)getInfo
{
for (int i=0;i<self.friendArr.count;i++)
{
EMConversation *conversation=[[EMClient sharedClient].chatManager getConversation:self.friendArr[i] type:EMConversationTypeChat createIfNotExist:YES];
messArr = [conversation loadMoreMessagesFromId:self.friendArr[i] limit:3 direction:EMMessageSearchDirectionUp];
EMMessage *mgs=messArr.lastObject;
NSMutableDictionary *infoDic=[NSMutableDictionary new];
[infoDic setObject:[MessageTool timeStr:mgs.timestamp] forKey:@"ChatTime"];
[infoDic setObject:self.friendArr[i] forKey:@"BossTel"];
[self.infoArr addObject:infoDic];
}
}
3.当有最新消息时,则改变未读数量
在AppDelegate页面写个通知:(或一开始程序启动页)
[[NSNotificationCenter defaultCenter]postNotificationName:@"messCount" object:nil userInfo:@{@"allMes":aMessages}];
在聊天列表页注册通知:
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(badgeCount:) name:@"messCount" object:nil];
记得最后注销通知:
[[NSNotificationCenter defaultCenter]removeObserver:self name:@"messCount" object:nil];
#pragma mark 如果有新的消息则改变未读数量
-(void)badgeCount:(NSNotification*)noty
{
//显示最新的那条数据
[messArr arrayByAddingObjectsFromArray:noty.userInfo[@"allMes"]];
[self.mesTabV reloadData];
for (int i=0; i<self.friendArr.count; i++)
{
EMConversation *conversation=[[EMClient sharedClient].chatManager getConversation:self.friendArr[i] type:EMConversationTypeChat createIfNotExist:YES];
//未读数判断
int unReadNum=[conversation unreadMessagesCount];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0];
NewMesTableViewCell *cell=[self.mesTabV cellForRowAtIndexPath:indexPath];
if (unReadNum>0)
{
cell.mes_UnReadNumBtn.hidden=NO;
if (unReadNum>99)
{
[cell.mes_UnReadNumBtn setTitle:@"99+" forState:UIControlStateNormal];
}
else
{
[cell.mes_UnReadNumBtn setTitle:[NSString stringWithFormat:@"%d",unReadNum] forState:UIControlStateNormal];
}
}
}
}
4.在tableview的代理中使用数据
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NewMesTableViewCell *newMesCell=[tableView dequeueReusableCellWithIdentifier:newMesCellStr];
if (!newMesCell)
{
newMesCell=[[[NSBundle mainBundle]loadNibNamed:@"NewMesTableViewCell" owner:self options:nil]lastObject];
}
newMesCell.delegate=self;
newMesCell.selectionStyle=UITableViewCellSelectionStyleNone;
//boss名字
newMesCell.mes_BossNameLab.text=self.friendArr[indexPath.row];
//会话
EMConversation *conversation=[[EMClient sharedClient].chatManager getConversation:self.friendArr[indexPath.row] type:EMConversationTypeChat createIfNotExist:YES];
messArr = [conversation loadMoreMessagesFromId:nil limit:3 direction:EMMessageSearchDirectionUp];
//未读数判断
int unReadNum=[conversation unreadMessagesCount];
if (unReadNum>0)
{
newMesCell.mes_UnReadNumBtn.hidden=NO;
[newMesCell.mes_UnReadNumBtn setTitle:[NSString stringWithFormat:@"%d",unReadNum] forState:UIControlStateNormal];
}
else
{
newMesCell.mes_UnReadNumBtn.hidden=YES;
}
//cell赋值
[newMesCell cellForValueWith:messArr.lastObject andCell:newMesCell];
return newMesCell;
}
5.在cell中赋值,判断最后一条消息类型及时间
//cell赋值
-(void)cellForValueWith:(EMMessage*)message andCell:(NewMesTableViewCell*)cell
{
//最后一条聊天记录
cell.mes_ChatLab.text=[self lastMesForValueWith:message];
//最后一条时间
cell.mes_TimeLab.text=[MessageTool timeStr:message.timestamp];
}
//MessageTool timeStr可以查看[环信3.0项目中用到的一些封装的类方法](http://www.jianshu.com/p/440a986c846d)
#pragma mark最后一条消息类型判断
-(NSString*)lastMesForValueWith:(EMMessage*)lastMessage
{
EMMessageBody *mesBody = lastMessage.body;
switch (mesBody.type)
{
case EMMessageBodyTypeText://文字
{
EMTextMessageBody *lastTextBody = (EMTextMessageBody *)mesBody;
NSString *text = lastTextBody.text;
return text;
}
break;
case EMMessageBodyTypeImage://图片
return @"[图片]";
break;
case EMMessageBodyTypeVoice://语音
return @"[语音]";
break;
case EMMessageBodyTypeLocation://位置
{
EMLocationMessageBody *lastLocationBody=(EMLocationMessageBody *)mesBody;
NSString *text=lastLocationBody.address;
return text;
}
default:
break;
}
return nil;
}
网友评论