ios - tableView重新加载时如何解决键盘问题?
问题描述
我在键盘和表格视图之间有问题。单击单元格时,API 调用并获取新数据。同时单击单元格键盘打开并在 tableview.reloadData() 中出现滚动问题。我正在调用单击单元格-> api 调用,获取新数据-> tableView 重新加载-> tableview 滚动到顶部,然后键盘 Willshow 函数将单元格带到键盘的顶部(稍后滚动到底部)。我该如何解决?
ps:每次都要调用API。
fileprivate func addingNotifications() {
//Subscribe to a Notification which will fire before the keyboard will show
subscribeToNotification(UIResponder.keyboardWillShowNotification, selector: #selector(keyboardWillShowOrHide))
//Subscribe to a Notification which will fire before the keyboard will hide
subscribeToNotification(UIResponder.keyboardWillHideNotification, selector: #selector(keyboardWillShowOrHide))
}
@objc func keyboardWillShowOrHide(notification: NSNotification) {
// Get required info out of the notification
if let scrollView = tableView, let userInfo = notification.userInfo, let endValue = userInfo[UIResponder.keyboardFrameEndUserInfoKey], let durationValue = userInfo[UIResponder.keyboardAnimationDurationUserInfoKey], let curveValue = userInfo[UIResponder.keyboardAnimationCurveUserInfoKey] {
// Transform the keyboard's frame into our view's coordinate system
let endRect = view.convert((endValue as AnyObject).cgRectValue, from: view.window)
// Find out how much the keyboard overlaps our scroll view
let keyboardOverlap = scrollView.frame.maxY - endRect.origin.y
// Set the scroll view's content inset & scroll indicator to avoid the keyboard
scrollView.contentInset.bottom = keyboardOverlap + 50
scrollView.scrollIndicatorInsets.bottom = keyboardOverlap + 50
let duration = (durationValue as AnyObject).doubleValue
let options = UIView.AnimationOptions(rawValue: UInt((curveValue as AnyObject).integerValue << 16))
UIView.animate(withDuration: 0, delay: 0, options: options, animations: {
self.view.layoutIfNeeded()
}, completion: nil)
}
}
解决方案
我不确定您在这里期待什么样的行为,但是如果您想辞职/隐藏键盘,那么您可以在 tableview 中编写 self.view.endEditing() 将选择委托方法(告诉委托一行即将成为选择)
推荐阅读
- c - 指向 C 函数中的指针的指针
- c - C 程序 fork() 结果重复
- sql - 获取新用户和回访用户细分(新付款人与回访付款人)
- android - Azure B2C Android 登录 AppAuth 演示 - 页面刷新而不是登录并且不重定向到应用程序
- flutter - Flutter 如何监听其他类的变量
- json - JSON - 如何使用 JQ 打印特定值
- swift - Swift 中的 Web 文本抓取器
- kubernetes - 将数据存储在自定义 Kubernetes 资源中的哪个位置更好
- javascript - 使用 JavaScript 和 Google Apps 脚本处理多个文件上传并将它们写入谷歌表格
- javascript - 单击列表项(javascript、css、html)后关闭导航菜单