2015-11-06 56 views
1

我收到錯誤消息: let indexPath = self.menuTable.indexPathForSelectedRow()!。 似乎我沒有從indexPathForSelectedRow獲得價值。我從CSV文件解析到Core Data。不知道它是否重要。我是編碼新手,所以不確定我是否缺少明顯的東西。致命錯誤:在解包可選值時意外發現零:Swift,核心數據

import UIKit 
import CoreData 

class MenuTableViewController: UITableViewController { 

    @IBOutlet var menuTable: UITableView! 
    private var menuItems:[MenuItem] = [] 
    var fetchResultController:NSFetchedResultsController! 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     // Load menu items from database 
     if let managedObjectContext = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext { 
      let fetchRequest = NSFetchRequest(entityName: "MenuItem") 
      var e: NSError? 
      menuItems = managedObjectContext.executeFetchRequest(fetchRequest, error: &e) as! [MenuItem] 
      if e != nil { 
       println("Failed to retrieve record: \(e!.localizedDescription)") 
      } 
     } 

     // Make the cell self size 
     self.tableView.estimatedRowHeight = 66.0 
     self.tableView.rowHeight = UITableViewAutomaticDimension 
     self.tableView.layoutIfNeeded() 
    } 

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

    // MARK: - Table view data source 

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
     // Return the number of sections. 
     return 1 
    } 

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     // Return the number of rows in the section. 
     return menuItems.count 
    } 

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
     let cell = menuTable.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! MenuTableViewCell 

     // Configure the cell... 
     cell.nameLabel.text = menuItems[indexPath.row].name 
     cell.detailLabel.text = menuItems[indexPath.row].detail 
//  cell.priceLabel.text = "$\(menuItems[indexPath.row].price as! Double)" 

     return cell 
    } 

    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) 
    { 
     self.performSegueWithIdentifier("showFront", sender: self) 

    } 

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) 
    { 
     if (segue.identifier == "showFront") 
     { 
      var upcoming: CardFrontViewController = segue.destinationViewController as! CardFrontViewController 

      let indexPath = self.menuTable.indexPathForSelectedRow()! 

      let titleString = menuItems[indexPath.row].name 

      upcoming.titleStringViaSegue = titleString 

      self.menuTable.deselectRowAtIndexPath(indexPath, animated: true) 

     } 
    } 

} 
+0

你的故事板中的單元格是否與segue相連? –

+0

是的,我將單元連接到故事板segue,「顯示(例如Push)」,標識符爲「showFront」。 – Sara

回答

0

既然你的tableView:didSelectRowAtIndexPath:的實施和單元連接到故事板的SEGUE中,SEGUE正在發生兩次。第二次執行segue時,將不會有選擇,因爲您在第一次segue期間取消選擇它。您可以通過刪除tableView:didSelectRowAtIndexPath:的實現或者通過在視圖控制器本身作爲源而不是單元格創建segue並讓手動調用segue來解決此問題。

+0

謝謝,謝謝,謝謝!我刪除了tableView:didSelectRowAtIndexPath:按照你的建議和它的工作! – Sara

+0

@Sara很高興能工作。如果解決了您的問題,您可以考慮將答案標記爲正確。 –

0

我不知道這是不是問題,但爲什麼你用自己作爲發件人,如果你需要indexPath?

嘗試:

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) 
    { 
     self.performSegueWithIdentifier("showFront", sender: indexPath) 

    } 

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) 
    { 
     if (segue.identifier == "showFront") 
     { 
      var upcoming: CardFrontViewController = segue.destinationViewController as! CardFrontViewController 


      let titleString = menuItems[indexPath.row].name 

      upcoming.titleStringViaSegue = titleString 

      self.menuTable.deselectRowAtIndexPath(indexPath, animated: true) 

     } 
    } 
+0

如果我不使用自己作爲發件人,會有什麼替代方法?我是新來的。我認爲這是最好的方式?... – Sara

+0

我掏出自己,但我仍然得到相同的錯誤... – Sara

+0

'重寫func prepareForSegue(segue:UIStoryboardSegue,sender:AnyObject?) { if( segue.identifier ==「showFront」) { var即將推出:CardFrontViewController = segue.destinationViewController as! CardFrontViewController let indexPath = menuTable.indexPathForSelectedRow()! 讓titleString =的菜單項[indexPath.row]。名稱 upcoming.titleStringViaSegue = titleString self.menuTable.deselectRowAtIndexPath(indexPath,動畫:真) } } ' – Sara

0

我看你使用的是UITableViewController。在UITableViewController中,UITableView會自動爲您提供所需的插座。您可以通過代碼self.tableView訪問它。我的猜測是,你沒有連接IBOutlet你的UITableViewmenuTable。所以在展開時爲零的可選項不是indexPath而是UITableView

修復: 刪除您IBOutlet到處都使用menuTable變量和使用self.tableView代替。

+0

我刪除我的IBOutlet中並用於自我。改爲tableView。但我收到了同樣的錯誤...... – Sara

+0

「覆蓋FUNC prepareForSegue(SEGUE:UIStoryboardSegue,發件人:AnyObject) { 如果(segue.identifier == 」showFront「) {VAR 即將到來:CardFrontViewController = segue.destinationViewController爲!CardFrontViewController let indexPath = self.tableView.indexPathForSelectedRow()! 讓titleString =的菜單項[indexPath.row]。名稱 upcoming.titleStringViaSegue = titleString self.tableView.deselectRowAtIndexPath(indexPath,動畫:真) } } } ' – Sara

相關問題