点击之定义tableviewcell,弹出键盘,通过距离判断解决键盘遮挡问题;
核心代码是如果tableview上的cell判断被遮挡 可以通过改变contentInset和滚动tableview到指定cell来实现
#pragma mark -- table data delegate --
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 2;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if(section == 0){
if (self.mainModel.zhuti !=nil &&self.mainModel.zhuti.pp_id.length>0) {
return 1;
}else{
return 0;
}
}else{
return self.mainModel.pj.count;
}
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.section == 0) {
WZCPPingLunTopTabCell *cell = [tableView dequeueReusableCellWithIdentifier:@"WZCPPingLunTopTabCell" forIndexPath:indexPath];
if (self.mainModel.zhuti !=nil && self.mainModel.zhuti.pp_id.length>0) {
ArticlePingJiaMjModel *model = self.mainModel.zhuti;
cell.pjModel = model;
}else{
}
return cell;
}else{
WZCPHuiFuPingLunTabCell *cell = [tableView dequeueReusableCellWithIdentifier:@"WZCPHuiFuPingLunTabCell"];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.vc = self;
ArticlePingJiaMjModel *model = self.mainModel.pj[indexPath.row];
cell.pjModel = model;
return cell;
}
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[tableView deselectRowAtIndexPath:indexPath animated:NO];
[self.toolView.searchTF resignFirstResponder];
if (indexPath.section == 0) {
_to_ppid_stauts = NO;
} else {
ArticlePingJiaMjModel *model = self.mainModel.pj[indexPath.row];
self.cur_topp_id = model.pp_id;
_to_ppid_stauts = YES;
self.curIndex = indexPath;
}
[self.toolView.searchTF becomeFirstResponder];
}
//区头区尾高度
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
if (section == 0) {
return 0.000001;
}else{
return 45;
}
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
if (section == 0) {
UIView *headerV = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 0.000001)];
headerV.backgroundColor = [UIColor whiteColor];
return headerV;
}else{
UIView *headerV = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 45)];
headerV.backgroundColor = [UIColor whiteColor];
UILabel *titL = [[UILabel alloc]init];
[headerV addSubview:titL];
titL.font = TEXT_MIDD_S_FONT14;
titL.textColor = TEXT_HEX_BLOCK_66;
titL.text = @"全部评论";
[titL mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerY.mas_equalTo(headerV.mas_centerY);
make.left.mas_equalTo(headerV.mas_left).mas_offset(15);
}];
return headerV;
}
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
if (section == 0) {
return 0.000001;
}else{
return 45;
}
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
if (section == 0) {
UIView *footerV = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 0.000001)];
footerV.backgroundColor = [UIColor whiteColor];
return footerV;
}else{
UIView *footerV = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 40)];
footerV.backgroundColor = [UIColor whiteColor];
return footerV;
}
}
- (void)keyboardWillShow:(NSNotification *)notification {
NSDictionary* info = [notification userInfo];
//获取键盘起始位置、大小信息
CGRect kbRect = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
//获取键盘弹出动画时间
NSValue *animationDurationValue = [info objectForKey:UIKeyboardAnimationDurationUserInfoKey];
NSTimeInterval animationDuration;
[animationDurationValue getValue:&animationDuration];
//获取键盘弹出动画类型
NSValue *animationCurveValue = [info objectForKey:UIKeyboardAnimationCurveUserInfoKey];
UIViewAnimationCurve animationCurve;
[animationCurveValue getValue:&animationCurve];
[UIView setAnimationCurve:animationCurve];
if (_to_ppid_stauts == YES && self.curIndex!=nil &&self.curIndex.section!=0) {//判断是那种方式弹出的键盘
//下面cell的点击事件
UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow];
WZCPHuiFuPingLunTabCell *cell = [self.myTableView cellForRowAtIndexPath:self.curIndex];
CGRect selfFrame = [cell convertRect:cell.frame toView:keyWindow];
//将试图的Y坐标向上移动offset个单位,以使界面腾出开的地方用于软键盘的显示
CGFloat offSet = selfFrame.origin.y - (keyWindow.frame.size.height - kbRect.size.height);
if (offSet > 0.01) {
self.myTableView.contentInset = UIEdgeInsetsMake(0, 0, kbRect.size.height, 0);
[self.myTableView scrollToRowAtIndexPath:self.curIndex atScrollPosition:UITableViewScrollPositionBottom animated:YES];
}else{
[self.myTableView scrollToRowAtIndexPath:self.curIndex atScrollPosition:UITableViewScrollPositionBottom animated:YES];
self.myTableView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
}
}else{
self.myTableView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
}
@weakify(self);
[UIView animateWithDuration:animationDuration
animations:^{
@strongify(self);
[self.toolView mas_remakeConstraints:^(MASConstraintMaker *make) {
make.bottom.mas_equalTo(self.view.mas_bottom).mas_offset(-kbRect.size.height);
make.width.mas_equalTo(SCREEN_WIDTH);
make.left.mas_equalTo(self.view.mas_left);
make.bottom.mas_equalTo(self.toolView.bkgView.mas_bottom);
}];
}];
}
- (void)keyboardWillHide:(NSNotification *)notification {
_to_ppid_stauts = NO; //清空--控制 弹出的键盘判断
NSDictionary* info = [notification userInfo];
//获取键盘起始位置、大小信息
//获取键盘弹出动画时间
NSValue *animationDurationValue = [info objectForKey:UIKeyboardAnimationDurationUserInfoKey];
NSTimeInterval animationDuration;
[animationDurationValue getValue:&animationDuration];
//获取键盘弹出动画类型
NSValue *animationCurveValue = [info objectForKey:UIKeyboardAnimationCurveUserInfoKey];
UIViewAnimationCurve animationCurve;
[animationCurveValue getValue:&animationCurve];
[UIView setAnimationCurve:animationCurve];
self.myTableView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
@weakify(self);
[UIView animateWithDuration:animationDuration
animations:^{
@strongify(self);
// self.toolView.center = self.curPoint;
[self.toolView mas_remakeConstraints:^(MASConstraintMaker *make) {
make.bottom.mas_equalTo(self.view.mas_bottom);
make.width.mas_equalTo(SCREEN_WIDTH);
make.left.mas_equalTo(self.view.mas_left);
make.bottom.mas_equalTo(self.toolView.bkgView.mas_bottom);
}];
}];
}
#pragma mark ---textField代理事件---
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
NSLog(@"进入编辑状态");
//进行视图切换
return YES;
}
//开始输入文字
- (void)textFieldDidBeginEditing:(UITextField *)textField{
textField.returnKeyType = UIReturnKeySend;
}
//键盘确定搜索的点击事件
-(BOOL)textFieldShouldReturn:(UITextField *)textField {
NSLog(@"点击搜索的逻辑");
if (self.ppid.length>0) {
if (_to_ppid_stauts == YES) {
self.trans_topp_id = self.cur_topp_id;
} else {
self.trans_topp_id = @"";
}
[self sendPingLunSeverce];
} else {
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
[hud showTextOnly:@"没有获取到文章详细信息请稍后再试" action:nil];
}
[textField resignFirstResponder];
return YES;
}
- (void)textFieldDidEndEditing:(UITextField *)textField{
_to_ppid_stauts = NO;
}
关键函数:
1.计算 cell相对于View的高度 并计算出他是否被遮挡
UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow];
WZCPHuiFuPingLunTabCell *cell = [self.myTableView cellForRowAtIndexPath:self.curIndex];
CGRect selfFrame = [cell convertRect:cell.frame toView:keyWindow];
//将试图的Y坐标向上移动offset个单位,以使界面腾出开的地方用于软键盘的显示
CGFloat offSet = selfFrame.origin.y - (keyWindow.frame.size.height - kbRect.size.height);
2.给键盘留出空间
// 改变tableview内部空间
self.myTableView.contentInset = UIEdgeInsetsMake(0, 0, kbRect.size.height, 0);
3.让tableview滚动到相应的cell部
//滚动到cell的样式UITableViewScrollPositionBottom
[self.myTableView scrollToRowAtIndexPath:self.curIndex atScrollPosition:UITableViewScrollPositionBottom animated:YES];