我正在尝试同时做两件事: 1)如果带有UITextView的单元格被覆盖,则将自定义单元格的TableView滚动到更好的位置,并且 2)当单元处于活动状态时,使用IBOutlet调用某些函数,并在其停用时保存其文本。
我已经尝试在两个文件中调用委托方法,一个方法是从TableView控制器调用到自定义单元格,另一个方法是从自定义单元调用到TableView控制器。我也尝试过使TableView控制器成为单元格的类,但是Xcode不允许这样做。我也曾尝试从cell类中激活键盘功能,但尚未弄清楚该怎么做。
extension NoteCell: NoteBackDelegate {
func activateCell() {
counterLabel.isHidden = false
let textRemaining = String(240-(textView.text.count))
counterLabel.text = textRemaining
textView.textColor = UIColor.darkGray
}
func updateCell() {
updateDateUpdated = true
let textRemaining = String(240-(textView.text.count))
counterLabel.text = textRemaining
let size = textView.bounds.size
let newSize = textView.sizeThatFits(CGSize(width: size.width, height: CGFloat.greatestFiniteMagnitude))
if size.height != newSize.height {
UIView.setAnimationsEnabled(true)
tableView?.beginUpdates()
tableView?.endUpdates()
UIView.setAnimationsEnabled(true)
if let thisIndexPath = tableView?.indexPath(for: self) {
tableView?.scrollToRow(at: thisIndexPath, at: .bottom, animated: false)
}
}
}
func deactivateCell() {
textView.textColor = UIColor.lightGray
counterLabel.isHidden = true
}
}
extension TableViewController {
func registerForKeyboardNotifications() {
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: UIResponder.keyboardWillShowNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide), name: UIResponder.keyboardWillHideNotification, object: nil)
}
func deregisterFromKeyboardNotifications() {
NotificationCenter.default.removeObserver(self, name: UIResponder.keyboardWillShowNotification, object: nil)
NotificationCenter.default.removeObserver(self, name: UIResponder.keyboardWillHideNotification, object: nil)
}
@objc func keyboardWillShow(notification: NSNotification) {
var info = notification.userInfo!
let keyboardSize = (info[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue.size
let contentInsets : UIEdgeInsets = UIEdgeInsets(top: 0.0, left: 0.0, bottom: keyboardSize!.height, right: 0.0)
self.tableView.contentInset = contentInsets
self.tableView.scrollIndicatorInsets = contentInsets
var aRect : CGRect = view.frame
aRect.size.height -= keyboardSize!.height
if let activeField = activeNoteTextView {
if (!aRect.contains(activeField.frame.origin)){
self.tableView.scrollRectToVisible(activeField.frame, animated: true)
}
}
}
func textViewDidBeginEditing(_ textView: UITextView) {
self.noteBackDelegate?.activateCell()
}
func textViewDidChange(_ textView: UITextView) {
self.noteBackDelegate?.updateCell()
}
func textViewDidEndEditing(_ textView: UITextView) {
self.noteBackDelegate?.deactivateCell()
}
@objc func keyboardWillHide(notification: NSNotification) {
self.tableView.contentInset = UIEdgeInsets.zero
var info = notification.userInfo!
let keyboardSize = (info[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue.size
let contentInsets : UIEdgeInsets = UIEdgeInsets(top: 0.0, left: 0.0, bottom: -keyboardSize!.height, right: 0.0)
self.tableView.contentInset = contentInsets
self.tableView.scrollIndicatorInsets = contentInsets
}
}
我要做的另一件事是,在NoteCell类中您会注意到,当在其中键入文本时,该单元格应自动重新格式化为更高的格式。再说一次,所有这些事情都是靠它们自己起作用的...但是,在其中添加了自动滚动键盘,迫使我将TextView委托切换回Table View,这似乎使我的笔记单元格中的功能失效。
我希望自从我调用NoteBackDelegate以来,那些激活,停用功能可以在我的笔记上使用,但是它们没有运行。我认为这是因为它不知道要切换计数器标签和文本颜色更改的注释单元格,但是我丝毫不知道如何传递该信息。有任何想法吗?非常感谢!