Ios 如何在UITableVIew中重新保存数据,而不会在Swift3中导致同一UITextVIew的任何重复

Ios 如何在UITableVIew中重新保存数据,而不会在Swift3中导致同一UITextVIew的任何重复,ios,swift,textview,tableview,Ios,Swift,Textview,Tableview,Userdefaults保存的数据成功地从myTextViewCotroller传递到TextViewTableController,但并非完全成功。这是因为当我的文本视图(已经有一些数据)重新保存时,会导致重复 例如,如果第一次保存的数据是“你好,我喜欢百吉饼”,如果我将其编辑为“你好,我喜欢百吉饼和巧克力饼干”,然后重新保存, 我的TableView的0索引是“你好,我喜欢百吉饼和巧克力饼干” 我的TableView的第一个索引是“你好,我喜欢百吉饼” 重复执行此操作时,myTableVie

Userdefaults保存的数据成功地从my
TextViewCotroller
传递到
TextViewTableController
,但并非完全成功。这是因为当我的
文本视图
(已经有一些数据)重新保存时,会导致重复

例如,如果第一次保存的数据是“你好,我喜欢百吉饼”,如果我将其编辑为“你好,我喜欢百吉饼和巧克力饼干”,然后重新保存, 我的TableView的0索引是“你好,我喜欢百吉饼和巧克力饼干” 我的TableView的第一个索引是“你好,我喜欢百吉饼”

重复执行此操作时,my
TableView
中有多个相同文本的副本。这太烦人了,我真的想找出这个问题的原因。但是,我不知道如何修复这个bug

TextTableViewController:

  class TextTableViewController: UITableViewController {

override func viewDidLoad() {
    super.viewDidLoad()

    tableView.dataSource = self
    self.tableView.reloadData()
}

func saveTextData() -> [String] {
    if let textData = userTextDataSave.array(forKey: "txtData") as? [String] {
        return textData
    }
    return []
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

// MARK: - Table view data source

override func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return saveTextData().count
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cellForText", for: indexPath)
    cell.backgroundColor = UIColor.clear
    cell.preservesSuperviewLayoutMargins = false
    cell.textLabel?.text = saveTextData()[indexPath.row]
    cell.textLabel?.font = UIFont.systemFont(ofSize: 20)
    tableView.reloadData()
    return cell
}

override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return 50
}
override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {

    return true
}
// Override to support editing the table view.
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {

    if editingStyle == .delete {

        // Delete the row from the data source
        removeHistory(index: indexPath.row)
        tableView.deleteRows(at: [indexPath], with: .fade)

    } else if editingStyle == .insert {
    }
}

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    performSegue(withIdentifier: "text",sender: nil)
}

override func prepare(for segue: UIStoryboardSegue, sender: Any!) {
    if (segue.identifier == "text") {
        let subVC: TextViewController = (segue.destination as? TextViewController)!
        let indexPath = tableView.indexPathForSelectedRow;
        subVC.textFromCell = saveTextData()[(indexPath?.row)!]
    }
}
}

TextViewController和用于保存文本数据的函数:

func save(){

    let alert = UIAlertController(title: "titile", message: "save?", preferredStyle: .alert)

    let noAction = UIAlertAction(title: "Cancel", style: .default, handler: { Void in
    })

    let okAction = UIAlertAction(title: "Save", style: .default, handler: { Void in

        self.addTextData(text: self.myTextView.text)
    })

    alert.addAction(noAction)
    alert.addAction(okAction)
    present(alert, animated: false, completion: nil)

}

func saveTextData() -> [String] {
    if let textData = userTextDataSave.array(forKey: "txtData") as? [String] {
        return textData
    }
    return []

}

func addTextData(text: String) {
    var data = saveTextData()
    for d in data {

        if d == "" {
            return
        }
    }
    data.insert(text, at: 0)
    userTextDataSave.set(text, forKey: "txtData")
    userTextDataSave.synchronize()
}
试试这个:

   func addTextData(text: String) {
var data = saveTextData()
for d in data {

    if d == "" {
        return
    }
    if d == text {
        return
    }
}

userTextDataSave.set(text, forKey: text)
userTextDataSave.synchronize()
}

为什么在将数据设置为用户默认值之前要在数组中插入文本?这是什么意思?我怎么做错了?你能给我看一个合适的代码吗?检查我的答案为什么每次都在第0个索引处保存文本?您似乎还没有共享完整的代码。@adeel我编辑并添加了更多信息。如果您仍然觉得它不够完善,请指出您认为需要了解的部分中的代码。这是否意味着我应该删除数据。insert()?不。但我想创建不同的文本数据。所以只有已经创建的文本数据不应该被重新创建。您更改了什么?您的意思是我应该在数据{if d==“”{return}}userTextDataSave.set中为d编写像efunc addTextData(text:String){var data=saveTextData()(数据,forKey:“txtData”)userTextDataSave.synchronize()}