当UITableViewCell里有UITextfield,当输入时键盘遮盖了UITextField,UITableView是会自动上移,当如果要让tableView自动滚动的话,还需要设置一下tableView的contentInset。接下来介绍一下实现步骤,
首先监听键盘出现和消失:
[[
NSNotificationCenter
defaultCenter
]
addObserver
:
self
selector
:
@selector
(keyboardWillShow:)
name
:
UIKeyboardWillShowNotification
object
:
nil
];
[[
NSNotificationCenter
defaultCenter
]
addObserver
:
self
selector
:
@selector
(keyboardWillHide:)
name
:
UIKeyboardWillHideNotification
object
:
nil
];
具体实现方法:
-(
void
)keyboardWillShow:(
NSNotification
*)note
{
CGRect
keyBoardRect=[note.
userInfo
[
UIKeyboardFrameEndUserInfoKey
]
CGRectValue
];
self
.
myTableView
.
contentInset
=
UIEdgeInsetsMake
(
0
,
0
, keyBoardRect.
size
.
height
,
0
);
}
#pragma mark 键盘消失
-(
void
)keyboardWillHide:(
NSNotification
*)note
{
self
.
myTableView
.
contentInset
=
UIEdgeInsetsZero
;
}