之前写这块时,服务器给返回头像和昵称,但是过了几天,后台又出问题,让自己写,欺负老实人啊!不和他们见识,把代码整理一下,能用,有问题可以直接留言。
环信的集成什么的都略过,直接看界面的代码
一.会话列表 如图
1.和之前说的一样,消息列表界面不需要自己写,直接继承环信的 EaseConversationListViewController并且遵守协议<EaseConversationListViewControllerDelegate, EaseConversationListViewControllerDataSource>,可以直接参考官方Demo(ios_IM_sdk_V3.3.2),找到文件Chat/ChatList/下的ConversationListController,可以把ConversationListController.h文件的方法直接Copy到自己的项目中,如图:(如果消息页只显示消息列表,没有自己的界面,则只需要继承即可,无需实现途中方法)
代码
@interface HYNMessageListController : EaseConversationListViewController<EaseConversationListViewControllerDelegate, EaseConversationListViewControllerDataSource,EMChatManagerDelegate,EMGroupManagerDelegate>
@property (weak, nonatomic) id<EaseConversationListViewControllerDelegate>delegate;
@property (weak, nonatomic) id<EaseConversationListViewControllerDataSource>dataSource;
@property (strong, nonatomic) NSMutableArray *conversationsArray;
- (void)refresh;
- (void)refreshDataSource;
- (void)isConnect:(BOOL)isConnect;
- (void)networkChanged:(EMConnectionState)connectionState;
-(void)setupUnreadMessageCount;
@end
2.在viewDidLoad中设置代理 如图
cell行单元中
代码
#pragma mark UITableViewDataSource---不同section的行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
if(section == 0){
return 3;
}else{
return [self.dataArray count];
}
}
#pragma mark UITableViewDataSource---行单元
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexPath.section == 0){
NSArray *imgArr = @[@"message_comment",@"message_thumbs",@"message_same_question"];
NSArray *titleArr= @[@"评论",@"点赞",@"同问"];
MyCell *cell = [MyCell cellWithTableView:tableView];
cell.itemImg.image = [UIImage imageNamed:imgArr[indexPath.row]];
cell.title.text = titleArr[indexPath.row];
//cell.pointImg.backgroundColor = UIColorFromRGB(0xF40707);
return cell;
}else{
NSString *CellIdentifier = [EaseConversationCell cellIdentifierWithModel:nil];
EaseConversationCell *cell = (EaseConversationCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the cell...
if (cell == nil) {
cell = [[EaseConversationCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
if ([self.dataArray count] <= indexPath.row) {
return cell;
}
id<IConversationModel> model = [self.dataArray objectAtIndex:indexPath.row];
cell.model = model;
if (self.dataSource && [self.dataSource respondsToSelector:@selector(conversationListViewController:latestMessageTitleForConversationModel:)]) {
NSMutableAttributedString *attributedText = [[self.dataSource conversationListViewController:self latestMessageTitleForConversationModel:model] mutableCopy];
[attributedText addAttributes:@{NSFontAttributeName : cell.detailLabel.font} range:NSMakeRange(0, attributedText.length)];
cell.detailLabel.attributedText = attributedText;
} else {
cell.detailLabel.attributedText = [[EaseEmotionEscape sharedInstance] attStringFro