如何在另一个ViewController中重新加载TableView的数据?

时间:2019-10-21 16:05:08

标签: swift tableview cell reloaddata

我已经创建了一个带有警报和名为saveAction的动作的viewController。当我单击警报中的saveActionButton时,它应该将一个字符串插入一个数组(存储在另一个类中),并且下一个viewController中的tableview应该重新加载数据和单元格的数量。 firstViewController,但它始终无法正常工作,总是出现错误:线程1:致命错误:意外地发现nil,同时隐式展开了Optional值。 你有什么想法吗?

code in the firstViewController



var tblv: UITableView? = nil
@IBAction func addButtonPressed(_ sender: Any) {

     buttonAnimation(button: addButton)
     singleToneClassData.passwordString.insert("\(passwordLabel.text ?? "")", at: 0)

        let alert = UIAlertController(title: "\(passwordLabel.text ?? "")", message: "customize your password", preferredStyle: .alert)

        alert.addTextField { (notesTextfield) in
            notesTextfield.placeholder = "type in some notes"
            notesTextfield.textAlignment = .center
        }
        alert.addTextField { (categoryTextfield) in
            categoryTextfield.placeholder = "choose your password category"
            categoryTextfield.textAlignment = .center
            categoryTextfield.inputView = self.picker
        }

        let actionSave = UIAlertAction(title: "save", style: .default) { (cancelButton) in
            self.dismiss(animated: true, completion: nil)
            self.singleToneClassData.passwordNote.insert("\(alert.textFields?.first?.text ?? "")", at: 0)

            print("passwordArray: \(self.singleToneClassData.passwordString)")
            print("passwordNotesArray: \(self.singleToneClassData.passwordNote)")
            print("passwordStringArray Länge: \(self.singleToneClassData.passwordString.count)")

            self.tblv?.reloadData()
//            self.secondController.tableView.reloadData()
        }

        let actionCancel = UIAlertAction(title: "cancel", style: .default) { (saveButton) in
            self.dismiss(animated: true, completion: nil)

        }

        alert.addAction(actionCancel)
        alert.addAction(actionSave)

        self.present(alert, animated: true)
    }





class where i store my data 

class passwordInformation {

    var passwordString = [String]()
    var passwordNote = [String]()
    var passwordCategoryText = [String]()
}




the tablevieClass


func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return singleToneClassData.passwordString.count

      }


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

        cell.passwordStringLabel.text = singleToneClassData.passwordString[indexPath.row]
        cell.notesTextfield.text = singleToneClassData.passwordNote[indexPath.row]
//        cell.categoryLabel.text = singleToneClassData.passwordCategoryText[indexPath.row]



        cell.backgroundColor = UIColor.green

        return cell
    }




override func viewDidLoad() {
        super.viewDidLoad()

        headLineLabel.text = "my password list"

        tableView.delegate = self
        tableView.dataSource = self

        let firstController = FirstViewController()
        firstController.tblv = tableView

        let nibName = UINib(nibName: "TableViewCell", bundle: nil)
        tableView.register(nibName, forCellReuseIdentifier: "cell")
    }


0 个答案:

没有答案