您的位置:首页 > 其它

实现汽泡聊天,动态计算高度和重新布局可以有在cell中实现,封装起来

2015-02-26 19:57 393 查看
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;

}

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