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