在ViewController中设置textview
的代理时,您需要在cellForRowAtIndexPath
中进行设置,并且可以在您的ViewController中访问textview的代理方法。
Swift 3代码片段如下:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
cell.yourTextView.delegate = self
}
当键盘出现/消失时,滚动tableview
是通过在viewDidLoad
中添加观察器来实现的,如下所示:
NotificationCenter.default.addObserver(self, selector:
NotificationCenter.default.addObserver(self, selector:
您可以使用以下方法检测键盘的出现/消失。
func keyboardWillShow(notification: NSNotification) {
print((notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as AnyObject).cgRectValue.size.height)
}
func keyboardWillHide(notification: NSNotification) {
}
不要忘记移除Observer的viewLifeCycle消失方法。
有一些第三方库可以实现这个功能,以下是其中的一些:
TPKeyboardAvoiding
IQKeyboardManager
[建议]
- 当你将tableview向上滚动时,请确保您插入到表格中的任何内容都会显示在tableview中。保持您的tableview这样,以便无论何时您输入任何单个内容在tableview cell中,用户都必须能够看到它。