微信聊天
2015-05-05 15:30
591 查看
1.要用到MVC代理模式2.要根据消息内容计算单元格的高度3.在输入框中输入内容添加到单元格上去controller//加载数据NSString *filePath = [[NSBundle mainBundle] pathForResource:@"messages.plist" ofType:nil];NSArray *array = [NSArray arrayWithContentsOfFile:filePath];_data = [[NSMutableArray array] retain];//将数据存储到model中for (NSDictionary *dic in array) {//创建model对象,将字典中的数据存储到model中Message *message = [[Message alloc] init];message.content = [dic objectForKey:@"content"];message.icon = [dic objectForKey:@"icon"];message.isSelf = [[dic objectForKey:@"self"] boolValue];message.time = [dic objectForKey:@"time"];
//将message对象放在数组中 //一个message对象代表一条消息 [_data addObject:message]; }//创建tableView视图和textField输入框_tabelView.separatorStyle = UITableViewCellSeparatorStyleNone;//将键盘上的return按钮改为send_inputView.returnKeyType = UIReturnKeySend;_inputView.delegate = self;- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {return _data.count;}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {static NSString *intenty = @"UITableViewCell";MessageCell *cell = [tableView dequeueReusableCellWithIdentifier:intenty];if (cell == nil) {cell = [[MessageCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:intenty];}Message *message = _data[indexPath.row];//将model交给视图去显示[cell setMessage:message];return cell;}
<pre name="code" class="objc">//返回单元格高度- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{//取到message对象,计算高度Message *message = _data[indexPath.row];CGSize size = [message.content sizeWithFont:[UIFont systemFontOfSize:16] constrainedToSize:CGSizeMake(220, 9999) lineBreakMode:NSLineBreakByWordWrapping];return size.height + 40;}
<span style="font-size:32px;"></span>
<span style="font-size:32px;">View</span><span style="color:#003333;">//cell布局,赋值UIImage *image = [UIImage imageNamed:_message.icon]; //用户头像userimage.image = image;//2.计算聊天信息所占用的空间大小//此方法会显示警告,因为此方法在ios7中已不建议使用(但仍可以使用)CGSize size = [_message.content sizeWithFont:[UIFont systemFontOfSize:16] constrainedToSize:CGSizeMake(220, 9999) lineBreakMode:NSLineBreakByWordWrapping];//3.根据求得的大小设置lable的高度_lable.text = _message.content;//背景视图UIImage *img1 = [UIImage imageNamed:@"chatfrom_bg_normal.png"]; //绿色背景UIImage *img2 = [UIImage imageNamed:@"chatto_bg_normal.png"]; //白色 自己UIImage *bgImg = _message.isSelf ?img2:img1;bgImg = [bgImg stretchableImageWithLeftCapWidth:bgImg.size.width * .5 topCapHeight:bgImg.size.height * .7];_bgImage.image = bgImg;//布局子视图 需判断消息是否为自己发送if (_message.isSelf) {userimage.frame = CGRectMake(320 - 50, 10, 40, 40);_bgImage.frame = CGRectMake(20, 10, size.width + 30, size.height + 30);_lable.frame = CGRectMake(40, 20, size.width, size.height);}else{userimage.frame = CGRectMake(10, 10, 40, 40);_bgImage.frame = CGRectMake(60, 10, size.width + 30, size.height + 30);_lable.frame = CGRectMake(75, 20, size.width, size.height);}</span>最后
<p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: 'Heiti SC Light'; color: rgb(0, 132, 0);"><pre name="code" class="objc"><pre name="code" class="objc">//send按钮被点击时,调用的方法- (BOOL)textFieldShouldReturn:(UITextField *)textField {//1.获取用户输入的内容NSString *text = textField.text;//2.创建一个message对象Message *message = [[Message alloc] init];message.content = text;message.icon = @"icon01.jpg";message.isSelf = YES;//将message对象放入数组中[_data addObject:message];// //刷新表视图// [_tabelView reloadData];//取得最后一个单元格的下标NSInteger index = _data.count - 1;NSIndexPath*indexPath = [NSIndexPath indexPathForRow:index inSection:0];//在表视图的最后插入一个单元格[_tabelView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationRight];//滚动到最后一个单元格[_tabelView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];//将输入框设为空_inputView.text = nil;return YES;}
相关文章推荐
- 仿微信聊天功能实现
- Lance老师UI系列教程第六课->微信聊天气泡界面的实现
- HTML5仿手机微信聊天界面
- 将view剪切为各种形状的view(类似微信聊天图片展示)。
- 【如何恢复微信已删除的聊天记录?】
- 微信回应被删聊天记录被获取
- 微信 开发 聊天机器人设计方案 java语言 程序和数据库代码
- 微信ios版6.2更新 聊天记录迁移更快捷朋友圈可翻译
- 微信QQ聊天记录将成有效证据
- 如何破解微信加密数据库EnMicroMsg.db读取聊天消息
- Android高仿微信聊天界面代码分享
- android 仿微信聊天气泡效果实现思路
- 微信泡妞聊天技巧入门神贴
- Android 高仿微信实时聊天 基于百度云推送
- 仿微信聊天界面
- 微信视频聊天记录怎么录制
- 如何实现微信聊天气泡
- 实现类似QQ、微信聊天界面,标题栏固定,键盘不遮挡底部输入框
- 自己研究的新软件HappyTime(类似微信聊天)
- CSS3 仿微信聊天小气泡