2014-10-22 41 views
0

我使用XCode 6,針對iOS 8並在Swift中編碼。塞格沒有執行iOS8

我的故事板的控制器是這樣的:

Tab Bar > Navigation > Table View > Detail View 

Show Detail賽格瑞是從Table CellDetail View

單擊表格單元格時不會觸發prepareForSegue方法。儘管如此,從按鈕到細節的工作仍然很好。 performSegueWithIdentifierdidSelectRowAtIndexPath也工作正常。

我還創建了一個全新的測試項目來測試這個問題 - 控制器代碼如下所示:

import UIKit 

class TestTableController: UITableViewController 
{ 
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int 
    { 
     return 2; 
    } 

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell 
    { 
     let cellId = "testCell"; 
     var cell: UITableViewCell? = tableView.dequeueReusableCellWithIdentifier(cellId) as? UITableViewCell; 

     if (cell == nil) 
     { 
      cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: cellId); 
     } 

     cell!.textLabel!.text = "cell \(indexPath.item)"; 

     return cell!; 
    } 
} 

知道爲什麼這不起作用開箱?

P.S .:當使用Split View而不是Tab Bar時,相同的segue正常工作。

+0

請確保您運行的是Xcode 6/iOS 8.1,因爲我在8.0.x中遇到了很多與segues(特別是unwind segues)有關的問題,但它們現在都已經修復。 – Rog 2014-10-22 10:08:04

回答

0

我剛剛用一個示例項目對其進行了測試。它工作正常。你的電池連線一定有問題。

這是我的代碼。只顯示來自普通香草標籤欄項目模板的更改。

enter image description here

// FirstViewController.swift 
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return 2 
} 

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    var cell = tableView.dequeueReusableCellWithIdentifier("Cell") as UITableViewCell 
    cell.textLabel?.text = "Cell \(indexPath.row + 1)" 
    return cell 
} 

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 
    let detail = segue.destinationViewController as DetailController 
    let cell = sender as UITableViewCell 
    let indexPath = self.tableView.indexPathForCell(cell) 
    detail.detailItem = "Cell \(indexPath!.row + 1)" 
} 

//DetailController.swift 
class DetailController: UIViewController { 

    var detailItem : String! 
    @IBOutlet var label : UILabel! 

    override func viewWillAppear(animated: Bool) { 
     super.viewWillAppear(animated) 
     label.text = detailItem 
    } 
} 
+0

是的,這正是我所做的......它在故事板的XML中看起來如何?你是否運行iOS 8.0或8.1? XCode 6.0還是beta? – JMicro 2014-10-22 11:20:14

+0

6.0.1,8.0。沒有仔細閱讀XML - 只是做了明顯的事情。 – Mundi 2014-10-22 11:34:26

+0

我已經刪除了單元格的ID,在IB中將其重新設置爲相同的值,並開始工作(WTF?)... 將您的答案標記爲正確,因爲您確認我的代碼沒有任何問題,並導致我陷入混亂細胞。 – JMicro 2014-10-22 12:02:38

0

調用prepareForSegue您必須添加一個SEGUE連接原型細胞和目標控制器的方法!方法不會調用,如果您以編程方式導航到目標控制器!

+0

實際上,即使您以編程方式執行Segue,prepareForSegue也會被調用。我希望它在沒有編程調用'performSegue'的情況下工作,因爲它應該... – JMicro 2014-10-22 11:22:41

1

當我在ViewDidLoad方法中使用tableView.register(AnyClass?, forCellReuseIdentifier: String)註冊單元格時,發生了這種情況當我評論此行並在故事板中添加單元格類和單元格標識符時,它開始工作。同樣,如果我在viewdidload中取消註釋該代碼,它將停止工作。

+0

這指出了我朝着正確的方向發展。我從一個xib實現切換到storyboard,我仍然使用自定義單元的xib,而不是將它定義爲原型。 – 2017-04-22 01:01:35