鍵盤迴收,tableview上的cell判斷被鍵盤遮擋解決方案
阿新 • • 發佈:2019-01-27
點選之定義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];