2017-03-09 81 views
-3

我已经在Stack上看到了关于这个问题的一些答案,但其中很多都来自5年前,并使用旧版本的Swift。我希望每次用户点击保存时重新加载表中的数据,以便现在将附加事件添加到表中。但不管我看起来什么都不做,什么都显示出来! 我试过在我的保存功能中加入table.reloadData()。但我得到了一个错误:如何在我的TableView中重新加载数据

Thread 1 error, Fatal error: unexpectedly found a nil while unwrapping an optional value.

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate { 
    @IBOutlet weak var txtTitle: UITextField! 
    @IBOutlet weak var txtLocation: UITextField! 
    @IBOutlet weak var txtDate: UITextField! 
    @IBOutlet weak var txtTime: UITextField! 

    var eventsArray = [Event]() 

    @IBAction func btnSave() { 
     let event = Event(tits: txtTitle.text!, locs: txtLocation.text!) 

     eventsArray.append(event) 
     table.reloadData() 
     print(eventsArray) 

    } 

    @IBOutlet weak var table: UITableView! 

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

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

     cell.title.text = eventsArray[indexPath.row].title 
     cell.location.text = eventsArray[indexPath.row].location 

     return cell 
    } 
} 
+2

你试图重新加载表的位置在哪里? – rmaddy

+0

我不断尝试从错误table.reloadData() –

+1

显示代码,你试过它,并显示你得到了什么“错误”。 – matt

回答

0

你设置的tableView的委托对viewDidLoad中?

table.delegate = self 
相关问题