实现汽泡聊天,动态计算高度和重新布局可以有在cell中实现,封装起来
来源:互联网 发布:ubuntu g 升级 编辑:程序博客网 时间:2024/05/21 07:00
1. (void)dealloc{ [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil]; [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];} 2. (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. _chatView = [[UIView alloc] initWithFrame:CGRectMake(0, 440, 320, 40)]; _chatView.backgroundColor = [UIColor grayColor]; [self.view addSubview:_chatView]; UITextField * f = [[UITextField alloc] initWithFrame:CGRectMake(20, 5, 240, 30)]; f.tag = 10; f.borderStyle = UITextBorderStyleRoundedRect; [_chatView addSubview:f]; UIButton * b = [UIButton buttonWithType:UIButtonTypeSystem]; [b setTitle:@"发送" forState:UIControlStateNormal]; [b addTarget:self action:@selector(send) forControlEvents:UIControlEventTouchUpInside]; b.frame = CGRectMake(270,5,40,30); [_chatView addSubview:b];
当键盘即将抬起时 触发当前对象中的keyboardWillShow方法
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
// 必须在delloc中移除观察者身份
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHidden:) name:UIKeyboardWillHideNotification object:nil];_tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 480-40) style:UITableViewStylePlain];_tableView.delegate = self;_tableView.dataSource = self;
// 去掉分割线======== _tableView.separatorStyle = UITableViewCellSeparatorStyleNone; [self.view addSubview:_tableView];
_dataArr = [NSMutableArray array];[self.view bringSubviewToFront:_chatView];UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap)];[self.view addGestureRecognizer:tap];
}
(void)tap
{
[self.view endEditing:YES];
}(void)send
{
UITextField * f = (UITextField *)[_chatView viewWithTag:10];
[_dataArr addObject:f.text];
[_tableView reloadData];
// 滚动到最后一行
[_tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:_dataArr.count-1 inSection:0] atScrollPosition:UITableViewScrollPositionBottom animated:YES];
}
- (void)keyboardWillShow:(id)note
{
CGFloat height = [[[note userInfo] objectForKey:@”UIKeyboardFrameEndUserInfoKey”] CGRectValue].size.height;
// 动画调整_chatView的位置 与键盘同步
[UIView animateWithDuration:0.25 animations:^{ _chatView.frame = CGRectMake(0, 480-height-40, 320, 40);}];
}
- (void)keyboardWillHidden:(NSNotification *)note
{
[UIView animateWithDuration:0.25 animations:^{
_chatView.frame = CGRectMake(0, 480-40, 320, 40);
}];
}
pragma mark - UITableViewDataSource
(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return _dataArr.count;
}(UITableViewCell )tableView:(UITableView )tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString * cellId = @”cellID”;
TableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellId];
if (cell == nil) {
cell = [[TableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
}cell.content = _dataArr[indexPath.row];
cell.contentView.backgroundColor = [UIColor colorWithRed:211/255.0 green:219/255.0 blue:239/255.0 alpha:1.0];return cell;
}
// 气泡的高度必须根据 文字的内容高度+20
(CGFloat)tableView:(UITableView )tableView heightForRowAtIndexPath:(NSIndexPath )indexPath
{
NSString * str = _dataArr[indexPath.row];
return [str sizeWithFont:[UIFont systemFontOfSize:17] constrainedToSize:CGSizeMake(250, 2000)].height+20;
}
“`
- 实现汽泡聊天,动态计算高度和重新布局可以有在cell中实现,封装起来
- 使用Autolayout实现UITableView的Cell动态布局和高度动态改变
- [译]使用Autolayout实现UITableView的Cell动态布局和高度动态改变
- 使用Autolayout实现UITableView的Cell动态布局和高度动态改变
- 使用Autolayout实现UITableView的Cell动态布局和高度动态改变
- 使用Autolayout实现UITableView的Cell动态布局和高度动态改变
- 使用Autolayout实现UITableView的Cell动态布局和高度动态改变
- 使用Autolayout实现UITableView的Cell动态布局和高度动态改变
- 使用Autolayout实现UITableView的Cell动态布局和高度动态改变
- IOS 使用Autolayout实现UITableView的Cell动态布局和高度动态改变
- 使用Autolayout实现UITableView的Cell动态布局和高度动态改变
- 使用Autolayout实现UITableView的Cell动态布局和高度动态改变
- 使用Autolayout实现UITableView的Cell动态布局和高度动态改变
- 使用Autolayout实现UITableView的Cell动态布局和高度动态改变
- 使用Autolayout实现UITableView的Cell动态布局和高度动态改变
- 使用Autolayout实现UITableView的Cell动态布局和高度的动态改变
- 使用Autolayout实现UITableView的Cell动态布局和高度动态改变
- 使用Autolayout实现UITableView的Cell动态布局和高度动态改变
- MIT python课程记录
- UIButton的背景图片不能显示
- objective-c 内存管理2
- 最小生成树 prim 算法 与kruskal 算法
- 对互联网技术和产品的不成熟看法
- 实现汽泡聊天,动态计算高度和重新布局可以有在cell中实现,封装起来
- Mac版Android Studio的安装和使用
- 学术休假期 项目4-点和距离
- Android中使用反射通过资源名字找到资源ID的方法
- java中的内部类总结
- JavaScript 闭包 this
- MarkDown相关问题
- 设置事务模式
- Sybase系统表