您的位置:首页 > 移动开发 > 微信开发

微信聊天

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;}

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息