【发布时间】:2019-04-07 16:20:06
【问题描述】:
我有一个 tableView,第 0 行有一个 textField,第 3 行有 textView。我的 tableview 当前每次有键盘时都会向上滑动。当 tableView 向上滑动时,您看不到第 0 行内的 textField。如何为第 0 行禁用此功能而只保留第 3 行?我尝试使用 Protocol & Delegates 尝试仅为第 3 行封装函数,但这不起作用。
类 CreateEditItemController: UIViewController, CreateItemDescriptionCellDelegate {
@IBOutlet weak var tableView: UITableView!
func handleKeyboardShow(notification: NSNotification) {
if let keyboardSize = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue {
if self.view.frame.origin.y == 0 {
//self.view.frame.origin.y -= keyboardSize.height
self.view.frame.origin.y -= 200
}
}
}
func handleKeyboardHide(notification: NSNotification) {
if self.view.frame.origin.y != 0 {
self.view.frame.origin.y = 0
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
switch indexPath.row {
...
case 3:
let cell = tableView.dequeueReusableCell(withIdentifier: "CreateItemDescriptionCell", for: indexPath) as! CreateItemDescriptionCell
cell.delegate = self
return cell
default:
return tableView.cellForRow(at: indexPath)!
}
}
}
protocol CreateItemDescriptionCellDelegate: class {
func handleKeyboardShow(notification: NSNotification)
func handleKeyboardHide(notification: NSNotification)
}
class CreateItemDescriptionCell: UITableViewCell, UITextViewDelegate {
//IBOUTLETS
@IBOutlet weak var notesTextView: UITextView!
weak var delegate: CreateItemDescriptionCellDelegate?
override func awakeFromNib() {
super.awakeFromNib()
notesTextView.delegate = self
NotificationCenter.default.addObserver(self, selector: #selector(handleKeyboardShow), name: UIResponder.keyboardWillShowNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(handleKeyboardHide), name: UIResponder.keyboardWillHideNotification, object: nil)
}
@objc func handleKeyboardShow(notification: NSNotification) {
delegate?.handleKeyboardShow(notification: notification)
}
@objc func handleKeyboardHide(notification: NSNotification) {
delegate?.handleKeyboardHide(notification: notification)
}
}
【问题讨论】:
标签: swift user-interface uikit