即时通讯UI-聊天界面(UITableView显示左右两人聊天)
目录
1.创建UITableView对象并设置相关属性
2.创建cellModel模型
//枚举类型
typedef enum {
ChatMessageFrom = ,//来自对方的消息
ChatMessageTo //发给对方的消息 }ChatMesageType; #import <Foundation/Foundation.h> @interface ChatModel : NSObject
@property (nonatomic,assign)ChatMesageType messageType;//类型
@property (nonatomic,copy)NSString *icon;//图片
@property (nonatomic,copy)NSString *content;//内容 - (instancetype)initWithDic:(NSDictionary *)dic; + (instancetype)modelWithDic:(NSDictionary *)dic;
#import "ChatModel.h" @implementation ChatModel - (instancetype)initWithDic:(NSDictionary *)dic {
self = [super init];
if (self) {
self.icon = dic[@"icon"];
self.content = dic[@"content"];
self.messageType = [dic[@"messageType"] intValue];
}
return self;
} + (instancetype)modelWithDic:(NSDictionary *)dic {
return [[self alloc]initWithDic:dic];
}
3.计算自适应cell高度 ChatCellFrame
#import <Foundation/Foundation.h>
#import "ChatModel.h"
/**
* cell中的布局,计算高度,位置等。。。
*/
@interface ChatCellFrame : NSObject @property (nonatomic,assign)CGRect iconRect; //图标位置大小
@property (nonatomic,assign)CGRect chartViewRect;//内容位置大小
@property (nonatomic,strong)ChatModel *chartMessage;//数据模型
@property (nonatomic, assign) CGFloat cellHeight; //cell高度 @end
#define kIconMarginX 5
#define kIconMarginY 5 #import "ChatCellFrame.h" @implementation ChatCellFrame //重写set方法
- (void)setChartMessage:(ChatModel *)chartMessage { _chartMessage=chartMessage; CGSize winSize=[UIScreen mainScreen].bounds.size;
CGFloat iconX=kIconMarginX;
CGFloat iconY=kIconMarginY;
CGFloat iconWidth=;
CGFloat iconHeight=;
//当为类型1
if(chartMessage.messageType==ChatMessageFrom){ }
//当为类型2
else if (chartMessage.messageType==ChatMessageTo){
iconX=winSize.width-kIconMarginX-iconWidth;
} //图标的位置大小
self.iconRect=CGRectMake(iconX, iconY, iconWidth, iconHeight); CGFloat contentX=CGRectGetMaxX(self.iconRect)+kIconMarginX;
CGFloat contentY=iconY;
//设置字体大小
NSDictionary *attributes = @{NSFontAttributeName: [UIFont fontWithName:@"HelveticaNeue" size:]};
//文本自适应大小 CGSize contentSize=[chartMessage.content boundingRectWithSize:CGSizeMake(, MAXFLOAT) options: NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:attributes context:nil].size; if(chartMessage.messageType==ChatMessageTo){ contentX=iconX-kIconMarginX-contentSize.width-iconWidth;
} //View的大小位置
self.chartViewRect=CGRectMake(contentX, contentY, contentSize.width+, contentSize.height+); //cell高度
self.cellHeight=MAX(CGRectGetMaxY(self.iconRect), CGRectGetMaxY(self.chartViewRect))+kIconMarginX;
} @end
4.设置cell上视图(图片和文字)ChatCellView
#import <UIKit/UIKit.h>
#import "ChatModel.h"
@interface ChatCellView : UIView @property (nonatomic,strong)UIImageView *iconImageView;
@property (nonatomic,strong)UILabel *contentLabel;
@property (nonatomic,strong)ChatModel *chartMessage; @end
#import "ChatCellView.h"
#define kContentStartMargin 25
@implementation ChatCellView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) { self.iconImageView=[[UIImageView alloc]init];
self.iconImageView.userInteractionEnabled=YES;
[self addSubview:self.iconImageView]; self.contentLabel=[[UILabel alloc]init];
self.contentLabel.numberOfLines=;
self.contentLabel.textAlignment=NSTextAlignmentLeft;
self.contentLabel.font=[UIFont fontWithName:@"HelveticaNeue" size:];
[self addSubview:self.contentLabel]; }
return self;
} //重写frame
- (void)setFrame:(CGRect)frame {
[super setFrame:frame]; self.iconImageView.frame=self.bounds;
CGFloat contentLabelX=;
if(self.chartMessage.messageType==ChatMessageFrom){ contentLabelX=kContentStartMargin*0.8;
}else if(self.chartMessage.messageType==ChatMessageTo){
contentLabelX=kContentStartMargin*0.5;
}
self.contentLabel.frame=CGRectMake(contentLabelX, -, self.frame.size.width-kContentStartMargin-, self.frame.size.height);
} @end
5.在cell中添加视图,并将模型数据添加上去 ChatCell
#import <UIKit/UIKit.h>
#import "ChatModel.h"
#import "ChatCellFrame.h"
#import "ChatCellView.h"
@interface ChatCell : UITableViewCell @property (nonatomic,strong)ChatCellFrame *cellFrame; @end
#import "ChatCell.h" @interface ChatCell ()
@property (nonatomic,strong) UIImageView *icon;
@property (nonatomic,strong) ChatCellView *chartView;
@property (nonatomic,strong) ChatCellView *currentChartView;
@property (nonatomic,strong) NSString *contentStr;
@end @implementation ChatCell - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
self.icon = [[UIImageView alloc]init];
self.chartView =[[ChatCellView alloc]initWithFrame:CGRectZero];
[self.contentView addSubview:self.icon];
[self.contentView addSubview:self.chartView];
}
return self;
} - (void)setCellFrame:(ChatCellFrame *)cellFrame {
_cellFrame=cellFrame; ChatModel *chartMessage=cellFrame.chartMessage; self.icon.frame=cellFrame.iconRect; //将图标位置赋给icon
self.icon.image=[UIImage imageNamed:chartMessage.icon];//图标 self.chartView.chartMessage=chartMessage;
self.chartView.frame=cellFrame.chartViewRect; //将内容位置赋给chartView
[self setBackGroundImageViewImage:self.chartView from:@"chatfrom_bg_normal.png" to:@"chatto_bg_normal.png"];
self.chartView.contentLabel.text=chartMessage.content; //设置文字信息
} //根据不同类型更换不同的背景图 -(void)setBackGroundImageViewImage:(ChatCellView *)chartView from:(NSString *)from to:(NSString *)to
{
UIImage *normal=nil ;
if(chartView.chartMessage.messageType==ChatMessageFrom){ normal = [UIImage imageNamed:from];
normal = [normal stretchableImageWithLeftCapWidth:normal.size.width * 0.5 topCapHeight:normal.size.height * 0.7]; }else if(chartView.chartMessage.messageType==ChatMessageTo){ normal = [UIImage imageNamed:to];
normal = [normal stretchableImageWithLeftCapWidth:normal.size.width * 0.5 topCapHeight:normal.size.height * 0.7];
}
chartView.iconImageView.image=normal;
} - (void)awakeFromNib {
// Initialization code
} - (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated]; // Configure the view for the selected state
} @end
6.回到控制器,设置数据源,(这里用的假数据),添加数据模型,使用自适应高度,使用自定义cell
#pragma mark - 懒加载 - (NSArray *)array {
if (!_array) {
_array = [[NSMutableArray alloc]initWithObjects:
@{@"icon":@"icon01.jpg",
@"content":@"早上好",
@"messageType":@""},
@{@"icon":@"icon02.jpg",
@"content":@"早上好呀",
@"messageType":@""}, nil];
}
return _array;
} #pragma mark - 模型数据 - (void)initDataSource {
_dataSource = [[NSMutableArray alloc]init];
for (NSDictionary *dic in self.array) {
ChatCellFrame *chatFrame = [[ChatCellFrame alloc]init];
ChatModel *chatModel = [ChatModel modelWithDic:dic];
chatFrame.chartMessage = chatModel;
[_dataSource addObject:chatFrame];
}
}
#pragma mark - initView - (void)initView {
_tableView = [[UITableView alloc]initWithFrame:self.view.bounds];
_tableView.dataSource = self;
_tableView.delegate = self;
_tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
[_tableView registerClass:[ChatCell class] forCellReuseIdentifier:CELLID];
[self.view addSubview:_tableView];
} #pragma mark - <UITableViewDataSource,UITableViewDelegate> - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return _dataSource.count;
} - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { ChatCell *cell = [tableView dequeueReusableCellWithIdentifier:CELLID];
cell.cellFrame=_dataSource[indexPath.row];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
} -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return [_dataSource[indexPath.row] cellHeight];
}
效果图:
即时通讯UI-聊天界面(UITableView显示左右两人聊天)的更多相关文章
- Java使用socket实现两人聊天对话
import java.io.*; import java.net.ServerSocket; import java.net.Socket; import java.util.Scanner; /* ...
- java版两人聊天程序
server.java import java.io.*; import java.net.*; import java.text.SimpleDateFormat; import java.util ...
- layim即时通讯实例各功能整合
一.系统演示1.1 聊天窗体主界面演示 1.2 模拟两人在线聊天(点击图片查看演示视频) 1.3 在线演示> 在线演示,点击进入系统到这里,若是您想要的,接下来听我娓娓道来二.开发工具开发软件: ...
- Android—简单的仿QQ聊天界面
最近仿照QQ聊天做了一个类似界面,先看下界面组成(画面不太美凑合凑合呗,,,,):
- 一款Java开源的Springboot即时通讯 IM,附源码
# 开篇 电商平台最不能缺的就是即时通讯,例如通知类下发,客服聊天等.今天,就来给大家分享一个开源的即时通讯系统.如对文章不感兴趣可直接跳至文章末尾,有获取源码链接的方法. 但文章内容是需要你简单的过 ...
- UI进阶 即时通讯之XMPP好友列表、添加好友、获取会话内容、简单聊天
这篇博客的代码是直接在上篇博客的基础上增加的,先给出部分代码,最后会给出能实现简单功能的完整代码. UI进阶 即时通讯之XMPP登录.注册 1.好友列表 初始化好友花名册 #pragma mark - ...
- iOS 即时通讯 + 仿微信聊天框架 + 源码
这些你造吗? 即时通讯(IM),在IOS这片江湖里面已经算是一个老者了,我这小旋风也是在很早以前巡山的时候,就知道有即时通讯这个妖怪,以前也多多少少接触过一些,在造APP的时候用过,哎呀,说着说着就感 ...
- 【手把手教程】uniapp + vue 从0搭建仿微信App聊天应用:腾讯云TXIM即时通讯的最佳实践
基于uniapp + vue 实现仿微信App聊天应用实践,实现以下功能 1: 用户登陆 2: 聊天会话管理 3: 文本/图片/视频/定位消息收发 4: 贴图表情消息收发 5: 一对一语音视频在线通话 ...
- 对接融云即时通讯组件SDK,轻松实现App聊天室
我好像特别喜欢做聊天室类的东东,刚折腾完微软的SignalR又折腾App.本来想研究研究XMPP的,由于服务器的搭建问题,先采用一个第三方的吧,看看效果如何.听到弟弟说他们公司用到了融云,我也下载个S ...
随机推荐
- ORACLE 单实例完全卸载数据库
1.用oracle用户登录如果要再次安装, 最好先做一些备份工作.包括用户的登录脚本,数据库自动启动关闭的脚本,和Listener自动启动的脚本.要是有可能连创建数据库的脚本也保存下来 2.使用SQL ...
- Linux 删除文件后空间不释放磁盘空间
在Linux操作系统下,有个没用的文件占用了400G,于是删除掉. [root@yoon log]# rm -rf messages [root@yoon log]# df -hFilesystem ...
- 使用checked关键字处理“溢出”错误
在进行算术运算时,可以使用checked关键字有效处理溢出错误,使用checked关键字可能对程序的性能会有一点点的影响,这是一种以牺牲性能换取健康的做法. private void button1_ ...
- SQL Server基本操作积累
一.基本操作 1.将数据绑定到DataGridVirw控件上显示的数据列标题将会是数据库中的字段名称,可以在使用select语句时使用AS关键字将转化为列名的别名 select name AS 姓名 ...
- SaaS应用“正益工作”发布,为大中型企业轻松构建移动门户
6月24日,以“平台之上,应用无限”为主题的2016 AppCan移动开发者大会,在北京国际会议中心隆重举行,逾1500名移动开发者一起见证了此次大会盛况. 会上,在专家领导.技术大咖.移动开发者的共 ...
- jquery 源码学习(*)
最近在做日志统计程序,发现对方的程序是在Jquery基础上进行开发的,而公司的网站的框架是prototype.而且我也早就想了解一下Jquery源码,故决定研究Jquery源码,模拟它的方法 Jq ...
- [转]SOLID开发原则-面向对象
S.O.L.I.D是面向对象设计和编程(OOD&OOP)中几个重要编码原则(Programming Priciple)的首字母缩写. SRP The Single Responsibility ...
- Android编程: 界面组成、事件监听器
学习知识:界面组成.事件监听器 ====界面组成==== 1.用户界面的基本组件叫做View,都是继承android.view.View类,Android里面预定义很多基本的界面组件,比如 Butto ...
- Hibernate从入门到精通(十)多对多单向关联映射
上一篇文章Hibernate从入门到精通(九)一对多双向关联映射中我们讲解了一下关于一对多关联映射的相关内容,这次我们继续多对多单向关联映射. 多对多单向关联映射 在讲解多对多单向关联映射之前,首先看 ...
- [shell基础]——join命令
测试文本内容 # cat -n name1.txt 1 name1 alvin1 2 name2 alvin2 3 name3 alvin3 4 name4 alvin4 # cat -n name2 ...