簡體   English   中英

當我快速呈現我的UITableViewController時,我的應用崩潰

[英]My aplication crash when I present my UITableViewController in swift

這是我的UITableViewController

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cellId: String = "Cell"

    let cell = tableView.dequeueReusableCellWithIdentifier(cellId, forIndexPath: indexPath) as UITableViewCell

    let data: NSManagedObject = list[indexPath.row] as! NSManagedObject

    let nom = data.valueForKey("nom") as! String
    let temps = data.valueForKey("temps") as! String

    cell.textLabel?.text = "\(nom)".uppercaseString
    cell.detailTextLabel?.text = "à fait : \(temps)"
    cell.detailTextLabel?.font = cell.detailTextLabel?.font.fontWithSize(15.5)

    return cell
}

這是呈現我的UITableViewController的我的動作

func seeAllScore(){
--------- when I put it in comment My App Work Correctly

    let tableView = AllScoreViewController()
    let navigationController = UINavigationController(rootViewController: tableView)
    self.presentViewController(navigationController, animated: true, completion: nil)

--------- when I put it in comment My App Crash

let vc:AllScoreViewController = self.storyboard!.instantiateViewControllerWithIdentifier("AllScore") as! AllScoreViewController
    self.presentViewController(vc, animated: true, completion: nil)
}

這是我的輸出

2016-02-14 23:01:39.350 WhereIsCage[42787:16030964] *** Assertion failure in -[UITableView dequeueReusableCellWithIdentifier:forIndexPath:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit_Sim/UIKit-3512.30.14/UITableView.m:6564
2016-02-14 23:01:39.358 WhereIsCage[42787:16030964] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'unable to dequeue a cell with identifier Cell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'
*** First throw call stack:

目標是在我的UITableViewCotroler中創建一個UINavigationBar

我建議您在情節let tableView = AllScoreViewController()定義表視圖控制器,但直接使用let tableView = AllScoreViewController()實例化它。

而不是這樣做,您應該從情節提要中加載它。 看來您也嘗試過(我不清楚該測試是否成功)...

您當前的實例化將創建一個表視圖,但是不了解已注冊的單元格,因此會出現錯誤。

當你說這個

let tableView = AllScoreViewController()

您只是在創建一個新的視圖控制器實例,而該實例與故事板沒有任何連接。 這意味着在原型單元上沒有任何信息,並且未注冊重用標識符。

我了解您實際上要實現的目標是將表視圖嵌入導航控制器中。 去做這個:

  1. 在Interface Building中選擇表格視圖場景
  2. 從“編輯器”菜單中,選擇“嵌入->導航控制器”
  3. 選擇新的導航控制器場景,然后在Identity Inspector中為其提供一個Storyboard ID(例如navController)。
  4. 現在,您可以從情節提要中實例化帶有表視圖控制器的導航控制器:

     let navvc = self.storyboard!.instantiateViewControllerWithIdentifier("navController") as! UINavigationController // If you want to access the AllScoreViewController let scoreVC=navvc.topViewController as! AllScoreViewController self.presentViewController(navvc, animated: true, completion: nil) 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM