首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >TableViewController问题:不保存来自textView的用户输入

TableViewController问题:不保存来自textView的用户输入
EN

Stack Overflow用户
提问于 2018-12-13 22:52:15
回答 1查看 102关注 0票数 0

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

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

代码语言:javascript
复制
var onTextEntered: ((String?) -> ())!

func setup() {
    notesTextView.delegate = self
}

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

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

CellForRowAt的下一步

代码语言:javascript
复制
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中输入一个字母或数字),我就会在我使用cellTextindexPath.row = text数组的行上得到错误:“致命错误:索引超出范围”。请帮助或让我知道,如果我的理解过程是错误的,很乐意学习!

EN

回答 1

Stack Overflow用户

发布于 2018-12-14 00:25:03

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

代码语言:javascript
复制
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的文本存储在数组中)。

代码语言:javascript
复制
//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编写的。如果有什么不清楚的地方,请随时问我。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53764507

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档