在运行应用程序时编辑单元格时更新数组 [英] Update array while editing cell while running the App

查看:66
本文介绍了在运行应用程序时编辑单元格时更新数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在ViewController中有一个 tableView 一个名为 toDo 的数组,在TableView中有一个单元格,在该单元格中有textView。
textView是可编辑的(用户可以更改textView中的文本)。
用户更改文本后-我希望单元格将其保存到toDo数组,但是每当我重新加载数据时,文本就会消失。
这是我尝试的方法:

I've got a tableView in the ViewController & an array called toDo, in the TableView I've a cell and in the cell I got textView. the textView is editable (The user can change the text in the textView). After the user changes the text - I want the cell to save it to the toDo array, but whenever I reloaddata the text disappears. Here is what I tried:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell: TableViewCell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath:indexPath) as! TableViewCell
        cell.textField.text = toDo[indexPath.row]
        cell.textField.delegate = self
        return cell
    }

**我有一个测试按钮,只要单击该按钮即可重新加载数据。

**I have a got a test button that whenever I click it reload data.

推荐答案

尝试一下-在文本字段上设置标签,并实现 textFieldDidEndEditing:在重新加载表格视图之前更新模型。

Try this out - set the tag on the text field and implement textFieldDidEndEditing: to update your model before reloading the table view.

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell: TableViewCell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath:indexPath) as! TableViewCell
    cell.textField.text = toDo[indexPath.row]
    cell.textField.tag = indexPath.row
    cell.textField.delegate = self
    return cell
}

func textFieldDidEndEditing(textField: UITextField) {
    toDo[textField.tag] = textField.text
}

这篇关于在运行应用程序时编辑单元格时更新数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆