【问题标题】:TableViewController issue: won't save user input from textViewTableViewController 问题:不会保存来自 textView 的用户输入
【发布时间】:2019-05-14 19:49:54
【问题描述】:

更新: 所以这样做的目标是保存用户在单元格中的 UITextView 中输入的文本,以便为该特定单元格编号保存文本,并且不会复制、移动或删除文本。

按照建议,我正在尝试通过执行以下操作来处理自定义单元格内的 textViewdidChange 函数:

var onTextEntered: ((String?) -> ())!

func setup() {
    notesTextView.delegate = self
}

func textViewDidChange(_ textView: UITextView) {
    onTextEntered(notesTextView.text)
}

制作一个包含文本的字符串,然后在每次调用 textViewDidChange 时将文本添加到字符串中(在我们进行过程中尝试向自己解释这一点,所以如果我的解释需要,请纠正我)。

CellForRowAt 中的下一步

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cellIdentifier = "TableViewNotesCell"

    let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as! TableViewNotesCell





    cell.setup()

    cell.onTextEntered = {input in

        if let text = input {

            self.cellText[indexPath.row] = text // here is the error (index out of range)

        }


        if indexPath.row < self.cellText.count {
            cell.notesTextView.text = self.cellText[indexPath.row] ?? "placeholder"
        }
    }

    return cell

}

当我执行上述代码时,只要调用 textViewDidChange(当我在 textView 中键入单个字母或数字时),我就会收到错误:“致命错误:索引超出范围”在我使用的行上cellText[indexPath.row] = 文本数组。如果我对过程的理解有误,请帮助或让我知道很想学习!

【问题讨论】:

  • 我不明白你最终想要实现什么。您能否更清楚地解释您想要完成的具体目标是什么?
  • 是的:正如问题中所说,我有这个 tableViewController 的 3 个功能,其中一个不工作。 tableViewcontroller 我正在尝试创建一个 tableViewController,它在每个单元格中都有一个按钮和一个 textView,并具有以下功能: 1. 当工具栏中的条形按钮项目被按下时添加一个单元格(工作)当按钮被按下时,textView 变得不可编辑并选中标记向上和向下滚动单元格(即出列单元格)时出现在按钮顶部(工作),具有用户输入的单元格将单元格保存到 indexPath.row,从而将文本保存到 indexPath(不工作)
  • 我需要将 textView 的文本保存在单元格内,然后在再次显示(或出列)时将其添加到首先包含文本数据的单元格中。
  • 您只需复制并粘贴您的原始帖子。例如,“将单元格保存到 indexPath.row”之类的事情是没有意义的。您要做的只是从单元格中获取输入并将其添加到视图控制器中的某个集合中吗?
  • 我这样做的原因是,当我上下滚动用户输入重复的单元格时,会被删除或出现在不同的位置。所以我的解决方案是添加来自单元格内的 textView 的文本输入,将其保存在一个数组中,然后当单元格显示或出列时,如果它是正确的 indexPath 的单元格,它只会添加文本

标签: swift tableview


【解决方案1】:

您可以尝试保存每次编辑

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    // inside cellForRowAt

    cell.textView.delegate = self

    cell.textView.tag = indexPath.row

    return cell
}

@objc func textViewDidChange(_ tex: UITextView) { 

    cellEndEdit[tex.tag] = tex.text!

}

class VC:UIViewController,UITextViewDelegate {

为数组提供默认值

 var cellEndEdit = [String](repeating: "", count: numberOfRows)

【讨论】:

  • 好的,你在代码中所做的是 1.将 textView 委托给 TableViewController,2:将 textView 的标签设置为 indexPath.row 3.在 textViewDidChange 函数中设置将对象添加到文本数组?你能解释一下吗,而且每个字母输入的 textViewDidChange 都会更新,你的意思是使用 textViewDidEndEditing
  • 好的,但我已经在 tableViewController 中使用了该函数,这有关系吗?
  • 不起作用,textViewDidChange 错误的函数只能用于:...." 看看帖子底部的更新我已经在那里发布了代码 sn-p
  • 返回单元格在哪里?
  • 添加它,答案是骨架不是全部
【解决方案2】:

假设您的 tableView 有可变数量的单元格,并且所有单元格都有一个 UITextView,其内容应该被记录和索引,我建议创建一个自定义 UITableViewCell 来处理 textView 本身。

class CustomTableViewCell: UITableViewCell, UITextViewDelegate {

    @IBOutlet weak var textView: UITextView() 
    var onTextEntered: ((String?) -> ()) //that's a callback

    func setup() {
        textView.delegate = self
    }

    override func textViewDidChange(textView: UITextView) {
        onTextEntered(textView.text)
    }
}

由于您正在处理用户输入的排序列表,因此您应该准备好数组,您可以在其中存储和检索数据。因此,如果某些数据已经存在,请通过您的数组 grep 并填充应得的单元格。还要在此处定义 onTextEntered 回调,以告诉单元格在被调用时要做什么(在您的情况下,将 UITextView 的文本存储在您的数组中)。

//your carrier, if you store the already existing user inputs some where, map them in here
//depending on how you calculate the number of cells, this array has to have the same size so use the same logic for this
var yourStringCarrierArray: [String?] = [] 

override func tableView(_ tableview: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "YourTableViewCell", for: indexPath) as! CustomTableViewCell
    cell.setup()
    cell.onTextEntered = { input in
        if let text = input {
            self.yourStringCarrierArray[indexPath.row] = text
        }
    if indexPath.row < yourStringCarrierArray.count {
        cell.textView.text = yourStringCarrierArray[indexPath.row] ?? "placeholder string, because there's no input here so far"
    }
}

我希望这会有所帮助,或者至少给你一个新的视角,已经有一段时间了,我用 Swift 编码。如果有不清楚的地方,请随时问我。

【讨论】:

  • 您好,感谢您的帮助!我试着按照你的方式做,请回复上面的更新版本,谢谢! :D
【解决方案3】:

使用对象来保存字符串值,因为 swift 中的 String 是值类型。这是一个例子:

class TestViewController: UIViewController {

    @IBOutlet weak var tableView: UITableView!

    var dataArray: [CellData] = []

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.

        tableView.delegate = self
        tableView.dataSource = self

        for _ in 0..<20 {
            dataArray.append(CellData())
        }
    }


}

extension TestViewController: UITableViewDelegate, UITableViewDataSource {
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 20
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "TestTableViewCell", for: indexPath) as! TestTableViewCell

        cell.setData(data: dataArray[indexPath.row])

        return cell
    }
}

class TestTableViewCell: UITableViewCell {

    @IBOutlet weak var textView: UITextView!

    var data: CellData!

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
        textView.text = ""
        textView.delegate = self
    }

    func setData(data: CellData) -> Void {
        self.data = data
        self.textView.text = data.stringValue
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}

extension TestTableViewCell: UITextViewDelegate {
    func textViewDidChange(_ textView: UITextView) {
        self.data.stringValue = textView.text
    }
}

class CellData {

    var stringValue: String = ""

}

【讨论】:

    猜你喜欢
    • 2017-06-23
    • 2018-06-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-14
    相关资源
    最近更新 更多