在 swift 的 TableView 单元格中编辑 TextView
edit a TextView in a TableView cell in swift
我被卡住了:我有一个由我制作的 .xib 单元格填充的 TableView。这些单元格中的每一个都包含一个可编辑的 TextView。
我正在尝试将用户在这些 TextView 中输入的文本保存在我的 Firebase 数据库中。我不想实现任何按钮,文本应该在 TextView 编辑结束后立即保存。
我尝试将 .xib 文件中的 TextView 连接到 UITableViewCell class,但它不允许我将其作为 IBAction 连接,而只能作为插座或插座连接。
请帮助我,谢谢!
screenshot
- 您需要在您的
UITableViewCell
class.
- 将
UITextView
的 delegate
连接到单元格 class。
- 在
func textViewDidEndEditing(UITextView)
中做任何你想做的事,你需要在你的单元格 class 中实现。
我解决了用 TextFields 替换 TextViews 的问题,它们看起来可能相同但可以作为 IBAction 链接到 UITableViewCell.swift。
因此,我编写了代码来更新 IBAction 中数据库的 "comments" 部分:
@IBAction func commentTextFieldToggle(_ sender: UITextField) {
if commentTextField.text != "" {
let comment = commentTextField.text
// I declared the next 7 constants to retreive the exact position of the string "comment" that I want to change
let date = dateLabel.text!
let time = timeLabel.text!
let year = date.suffix(4)
let day = date.prefix(2)
let partialMonth = date.prefix(5)
let month = partialMonth.suffix(2)
//I use this "chosenDate" constant to retreive the database query that I previously saved using the date in the below format as index:
let chosenDate = "\(year)-\(month)-\(day) at: \(time)"
let commentsDB = Database.database().reference().child("BSL Checks")
commentsDB.child((Auth.auth().currentUser?.uid)!).child(String(chosenDate)).child("Comments").setValue(comment) {
(error, reference) in
if error != nil {
print(error!)
} else {
print("User Data saved successfully")
}
}
}
}
我被卡住了:我有一个由我制作的 .xib 单元格填充的 TableView。这些单元格中的每一个都包含一个可编辑的 TextView。 我正在尝试将用户在这些 TextView 中输入的文本保存在我的 Firebase 数据库中。我不想实现任何按钮,文本应该在 TextView 编辑结束后立即保存。
我尝试将 .xib 文件中的 TextView 连接到 UITableViewCell class,但它不允许我将其作为 IBAction 连接,而只能作为插座或插座连接。
请帮助我,谢谢!
screenshot
- 您需要在您的
UITableViewCell
class. - 将
UITextView
的delegate
连接到单元格 class。 - 在
func textViewDidEndEditing(UITextView)
中做任何你想做的事,你需要在你的单元格 class 中实现。
我解决了用 TextFields 替换 TextViews 的问题,它们看起来可能相同但可以作为 IBAction 链接到 UITableViewCell.swift。 因此,我编写了代码来更新 IBAction 中数据库的 "comments" 部分:
@IBAction func commentTextFieldToggle(_ sender: UITextField) {
if commentTextField.text != "" {
let comment = commentTextField.text
// I declared the next 7 constants to retreive the exact position of the string "comment" that I want to change
let date = dateLabel.text!
let time = timeLabel.text!
let year = date.suffix(4)
let day = date.prefix(2)
let partialMonth = date.prefix(5)
let month = partialMonth.suffix(2)
//I use this "chosenDate" constant to retreive the database query that I previously saved using the date in the below format as index:
let chosenDate = "\(year)-\(month)-\(day) at: \(time)"
let commentsDB = Database.database().reference().child("BSL Checks")
commentsDB.child((Auth.auth().currentUser?.uid)!).child(String(chosenDate)).child("Comments").setValue(comment) {
(error, reference) in
if error != nil {
print(error!)
} else {
print("User Data saved successfully")
}
}
}
}