2017-10-08 20 views
0

參數我在從一個視圖控制器一個textFieldShouldReturn手動調用cellForRowAtindexPath,以及用於cellForRowAtindexPath參數都是tableViewOutletindexPath(在這個例子中「索引」)。這裏的cellForRowAtindexPathtableViewOutlet都來自另一個視圖控制器,我使用實例調用它們。傳遞的tableview出口中的cellForRowAtIndexPath

一旦cellForRowAtindexPath調用它顯示錯誤,如:

unexpectedly found nil while unwrapping an optional value.

func textFieldShouldReturn(_ textField: UITextField) -> Bool { 
    textField.resignFirstResponder() 
    if(textField == carRating){ 
     let rating = Float(textField.text!) 
     secondInstanceForMyData.masterCarData[index.row].carRating = rating 
     let _: MyTableViewCell = myViewControllerInstance.tableView(myViewControllerInstance.tableViewWillLoad as UITableView, cellForRowAt: index) as! MyTableViewCell 
    } 
    return true 
} 
+1

可能的重複[什麼是「致命錯誤:意外地發現零,而解包可選值」是什麼意思?](https://stackoverflow.com/questions/32170456/what-does-fatal-error-unexpectedly-found- nil-while-unwrapping-an-optional-valu) – the4kman

+0

表示從我們提取的變量中沒有數據變量 – Harsha

+0

以'let _:MyTableViewCell = ...'開頭的行的要點是什麼? – rmaddy

回答

0

什麼你調用這裏是實現代碼如下數據源的方法tableView(tableView:cellForRowAt:),這不應該由你來調用,它必須通過實施您。想想這樣,這個方法需要爲表視圖提供一個單元格,而不是要求它。

如果你想從一個已填充的tableview細胞,調用實現代碼如下方法cellForRow(at:)

let indexPath = IndexPath(row: 0, section: 0) 
if let cell = tableView?.cellForRow(at: indexPath) as? MyTableViewCell { 
    // do something with the cell 
} 

如果你想更新您的tableview,那麼你需要更新您的數據源,要麼調用reloadData()在你的tableview上或者調用其中一種更新方法。

相關問題