如果UITableView的每一个cell中都有一个textField,点击时弹起的键盘挡住了该cell,则应该将tableView弹起一定距离,以避免被遮挡:
1、获取cell在tableview中的位置。
CGRect rectInTable = [_tableView rectForRowAtIndexPath:indexPath];
2、将cell在tableView中的位置转化为在屏幕中的位置。
当tableView滑动的时候, label在屏幕中的位置会发生变化 应该怎样去计算cell在屏幕中的位置呢,毕竟键盘是相对屏幕的,不是相对tableVUew的。
- (CGRect)convertRect:(CGRect)rect toView:(nullableUIView *)view;
以下是完整的代码:
- (void) keyboardWillShow : (NSNotification*)notification {
CGRect keyboardFrame = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey]CGRectValue];
NSIndexPath *indexPath = [NSIndexPathindexPathForRow:_firstResponderFieldIndexinSection:0];
CGRect rectInTable = [_tableView rectForRowAtIndexPath:indexPath];
CGRect rectInSelfview = [_tableView convertRect:rectInTable toView:self.view];
CGFloat cellBottomY = rectInSelfview.origin.y + rectInSelfview.size.height;
if (cellBottomY > keyboardFrame.origin.y ) { //键盘是否会挡住点击cell的判断
CGFloat delta = _tableView.frame.origin.y - (cellBottomY - keyboardFrame.origin.y);
_tableView.frame =CGRectMake(0, delta,SCREEN_WIDTH,_tableView.frame.size.height);
}
}
注:_firstResponderFieldIndex就是点击的cell的tag

当UITableView中的cell包含textField,点击弹出键盘可能遮挡cell。通过获取cell在tableView和屏幕中的位置,计算出避免键盘遮挡的调整值,从而实现tableView上移。关键方法包括`rectForRowAtIndexPath`和`convertRect:toView:`。
1407

被折叠的 条评论
为什么被折叠?



