我们这计算cell的高度,主要是内部UIlabel的高度。
如果label上文字已设置,就可以确定label的高度。
所以,我们在cell里提供一个方法:
//
// ChatCell.m
#import "ChatCell.h"
@implementation ChatCell
- (CGFloat)cellHeight
{
// 1.重新布局子控件(后label的高度已经确定了)
[self layoutIfNeeded];
// 2.返回cell的高度
return self.messageLabel.frame.size.height + 50;
}
@end
2.控制器里我们需要一个专门用来计算高度的cell,同样是ChatCell
/**
* 专门用来计算高度的cell工具对象
*/
@property(nonatomic,strong) ChatCell *chatCellTool;
然后创建这个工具cell
/**
* 懒加载创建chatCellTool
*/
- (ChatCell *)chatCellTool
{
if (!_chatCellTool) {
//Identifier 用recivierCell或senderCell都可以,因为2个cell其实内部
//Identifier 声明在cell内部的
_chatCellTool = [self.tableView dequeueReusableCellWithIdentifier:senderCell];
}
return _chatCellTool;
}
3.tableView代理方法中返回cell的高度
/**
* 返回cell的高度
*/
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
// 1.设置label的数据(计算高度之前,一定要给label数据)
self.chatCellTool.messageLabel.text = self.dataSources[indexPath.row];
// 2.返回cell的高度
return [self.chatCellTool cellHeight];
}
4.完整的控制器代码:
//
// ChatViewController.m
#import "ChatViewController.h"
#import "ChatCell.h"
@interface ChatViewController ()<UITableViewDataSource,UITableViewDelegate>
/**
* inputToolbar(输入工具栏)底部的约束
*/
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *inputToolbarBottomConstraint;
@property(nonatomic,strong)NSMutableArray *dataSources;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
/**
* 专门用来计算高度的cell工具对象
*/
@property(nonatomic,strong) ChatCell *chatCellTool;
@end
@implementation ChatViewController
- (NSMutableArray *)dataSources
{
if (!_dataSources) {
_dataSources = [NSMutableArray array];
}
return _dataSources;
}
/**
* 懒加载创建chatCellTool
*/
- (ChatCell *)chatCellTool
{
if (!_chatCellTool) {
//Identifier 用recivierCell或senderCell都可以,因为2个cell其实内部
//Identifier 声明在cell内部的
_chatCellTool = [self.tableView dequeueReusableCellWithIdentifier:senderCell];
}
return _chatCellTool;
}
- (void)viewDidLoad {
[super viewDidLoad];
[self.dataSources addObject:@"aldjaljgdlakdjgal;kdgja;lkdgja;"];
[self.dataSources addObject:@"al阿里的杰拉德加咖喱的空间旮旯的空间按了个凯迪拉克加咖喱块的架构拉开到加咖喱块大公鸡djgal;kdgja;lkdgja;"];
[self.dataSources addObject:@"al阿里的杰拉德加咖喱的空间旮旯的空间按了个凯迪拉克加咖喱块的架构拉开到加咖喱块大公鸡djgal;kdgja;lkdgja;"];
[self.dataSources addObject:@"al阿里的杰gja;"];
[self.dataSources addObject:@"al阿里的杰拉德加咖喱的空间旮旯的空间按了个凯迪拉克加咖喱块的架构拉开到加咖喱块大公kdgja;"];
[self.dataSources addObject:@"al阿里的杰拉德加咖喱的空间旮旯的空加咖喱块的架构拉开到加咖喱块大公鸡djgal;kdgja;lkdgja;"];
[self.dataSources addObject:@"al阿喱的空间旮旯的空间按了个凯迪拉克加咖喱块的架构拉开到加咖喱块大公鸡djgal;kdgja;lkdgja;"];
// 1.监听键盘的弹出
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
// 2.监听键盘的退出
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}
/**
* 键盘退出会调用
*/
- (void)keyboardWillHide:(NSNotification *)notice
{
// inputToolbar回到原位
self.inputToolbarBottomConstraint.constant = 0;
[UIView animateWithDuration:0.25 animations:^{
[self.view layoutIfNeeded];
}];
}
/**
* 键盘弹出会调用
*/
- (void)keyboardWillShow:(NSNotification *)notice
{
// 1.获取键盘高度
CGFloat heigth = [notice.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue].size.height;
// 2.更改inputToolbar底部约束
self.inputToolbarBottomConstraint.constant = heigth;
[UIView animateWithDuration:0.25 animations:^{
[self.view layoutIfNeeded];
}];
}
/**
* 添加通知要移除,我们可以在这里方法里移除
*/
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
#pragma mark -UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.dataSources.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
ChatCell *cell = nil;
if (indexPath.row % 2 == 0) {
cell = [tableView dequeueReusableCellWithIdentifier:senderCell];
}else{
cell = [tableView dequeueReusableCellWithIdentifier:recivierCell];
}
cell.messageLabel.text = self.dataSources[indexPath.row];
return cell;
}
#pragma mark - UITableViewDelegate
/**
* 返回cell的高度
*/
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
// 1.设置label的数据(计算高度之前,一定要给label数据)
self.chatCellTool.messageLabel.text = self.dataSources[indexPath.row];
// 2.返回cell的高度
return [self.chatCellTool cellHeight];
}
@end
5.自定义的ChatCell的代码:
//
// ChatCell.h
#import <UIKit/UIKit.h>
static NSString *recivierCell = @"recivierCell";
static NSString *senderCell = @"senderCell";
@interface ChatCell : UITableViewCell
@property (weak, nonatomic) IBOutlet UILabel *messageLabel;
/**
* 返回cell的高度
*/
- (CGFloat)cellHeight;
@end
//
// ChatCell.m
#import "ChatCell.h"
@implementation ChatCell
- (CGFloat)cellHeight
{
// 1.重新布局子控件(后label的高度已经确定了)
[self layoutIfNeeded];
// 2.返回cell的高度
return self.messageLabel.frame.size.height + 50;
}
@end
6.聊天界面布局效果: