![](/img/trans.png)
[英]Why does my app crash when I pick a song from my music library in Swift?
[英]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()
您只是在創建一個新的視圖控制器實例,而該實例與故事板沒有任何連接。 這意味着在原型單元上沒有任何信息,並且未注冊重用標識符。
我了解您實際上要實現的目標是將表視圖嵌入導航控制器中。 去做這個:
現在,您可以從情節提要中實例化帶有表視圖控制器的導航控制器:
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.