Ios 保存添加的tableview单元格

Ios 保存添加的tableview单元格,ios,swift,uitableview,Ios,Swift,Uitableview,我将如何保存已添加到表视图的单元格,以便在应用程序终止并重新启动时,它们仍然存在。还是有更简单的方法来实现这一点 我正在使用一个提醒,让用户输入信息,然后将其添加到表视图单元格中,我只需要它来保存单元格以便重新启动。所以它们仍然可见 struct MyLink: Codable{ var nickname: String var url: String } class TableView: UIViewController { let tableView: UITab

我将如何保存已添加到表视图的单元格,以便在应用程序终止并重新启动时,它们仍然存在。还是有更简单的方法来实现这一点

我正在使用一个提醒,让用户输入信息,然后将其添加到表视图单元格中,我只需要它来保存单元格以便重新启动。所以它们仍然可见

struct MyLink: Codable{
    var nickname: String
    var url: String
}

class TableView: UIViewController {

    let tableView: UITableView = {
        let tableView = UITableView()
        tableView.tableFooterView = UIView(frame: .zero)
        tableView.separatorStyle = .none
        tableView.allowsSelection = true
        tableView.backgroundColor = .clear
        tableView.translatesAutoresizingMaskIntoConstraints = false
        return tableView
    }()
    
    var nickNameString: String = ""
    var urlString: String = ""

    var links: [MyLink] = []

    override func viewDidLoad() {
        super.viewDidLoad()
        
        let add = UIBarButtonItem(image: UIImage(systemName: "plus"), style: .done, target: self, action: #selector(addLink))
        navigationItem.rightBarButtonItems = [add]
        
        constraints()
        
        tableView.dataSource = self
        tableView.delegate = self
        tableView.register(TableViewCell.self, forCellReuseIdentifier: "TVC")
        self.tableView.separatorColor = .clear

    }

    func constraints(){
        view.addSubview(tableView)
        
        NSLayoutConstraint.activate([
            tableView.topAnchor.constraint(equalTo: view.topAnchor, constant: 0),
            tableView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: 0),
            tableView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 0),
            tableView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: 0)
        ])
    }
    
    @objc func addLink(){
        var txtNickName: UITextField?
        var txtUrl: UITextField?
        
        let alert = UIAlertController(title: "Add Link", message: nil, preferredStyle: .alert)
                
        let action = UIAlertAction(title: "Add", style: .default) { (login) in
            if let nickname = txtNickName?.text {
                self.nickNameString = nickname
            } else {}
                    
            if let url = txtUrl?.text {
                self.urlString = url
            } else {}
            
            let addLink = MyLink(nickname: self.nickNameString, url: self.urlString)

            let index = 0
            self.links.insert(addLink, at: index)

            let indexPath = IndexPath(row: index, section: 0)
            self.tableView.insertRows(at: [indexPath], with: .fade)
        }
                
        alert.addTextField { (nickname) in
            txtNickName = nickname
            txtNickName?.placeholder = "Enter Nickname"
        }
                
        alert.addTextField { (url) in
            txtUrl = url
            txtUrl?.placeholder = "Enter Url"
        }
                
        alert.addAction(action)
        self.present(alert, animated: true, completion: nil)
    }

}

extension TableView: UITableViewDelegate, UITableViewDataSource {
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return links.count
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 100
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    }
    func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
 
    }
    func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
        guard editingStyle == .delete else {return}
        links.remove(at: indexPath.row)
        
        tableView.deleteRows(at: [indexPath], with: .automatic)
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell : TableViewCell = tableView.dequeueReusableCell(withIdentifier: "TVC", for: indexPath) as! TableViewCell
        
        cell.selectionStyle = .none
        
        let link = links[indexPath.row]
        cell.nameLabel.text = link.nickname
        cell.urlLabel.text = link.url
        
        return cell
    }
}

简单的解决方案是,您可以将用户输入数组保存在Preference或CoreData中。因此,当用户打开应用程序时,您可以填充数据。