2014-09-30 31 views
-1

我有一個簡單的故事板項目,有一個UITableViewController和一個細節VC。表視圖控制器有一個自定義類,它是表的委託和數據源。preprareForSegue:發件人:永遠不會被調用UITableViewController

我創建用下面的代碼的單元:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *cellId = @"cellId"; 



    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId]; 

    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle 
             reuseIdentifier:cellId]; 
    } 
    // Configure the cell... 


    return cell; 
} 

定義在故事板中的tableview如下:

enter image description here

它顯示正確地,然而,當我點擊一個單元格,沒有任何反應,並準備Segue:發件人:從來沒有被調用。

在故事板中,實現代碼如下電池連接到細節VC:

enter image description here

我已經檢查類似的問題,但似乎沒有適用於我的情況。 tableview的自定義類被設置,所有的委託和數據源接線似乎都沒問題。

我很新的故事板,所以我擔心錯誤必須在它的配置。如果我實現了didSelectRow ...我可以讓系統按預期工作,但我想用segues來完成。

+0

告訴我,你已經連接你的UITableViewController與詳細信息視圖使用push segue .. – 2014-09-30 14:44:03

+0

和seg應該在哪裏調用? 'performForSegue:'在代碼中?在故事板上,它連接在哪裏? – Larme 2014-09-30 14:44:17

+0

在你的故事板中,單擊單元格,並創建你的segue,以適應任何你想要嵌入的'ViewController'。 – 2014-09-30 14:44:24

回答

0

從您的UITableViewCell拖動並按住「ctrl」到您想要的視圖控制器。作爲segue類型使用push並給segue一個標識符。然後,你可以調用performSegue:segue,並檢查您的賽格瑞得到由

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { 
     if ([segue.identifier isEqualToString:@"your identifier"]) { 
     YourViewController *yourVC = (YourViewController*)segue.destinationViewController; 
     // prepare your vc 
     } 
} 

叫你也可以去掉勾選cell==nil,因爲這不再是必要的,因爲iOS的6

+0

我已經做到了。請看我更新的問題 – cfischer 2014-09-30 15:11:31

+0

@cfisher你已經刪除了單元格== nil check?您可以刪除整個if語句。 – gpichler 2014-10-01 14:29:35

0

準備賽格瑞只調用時執行SEGUE被稱爲或者是因爲你在故事板中有一個連接,或者你被稱爲明確執行segue。

對於調用執行segue - Ctr +從您的uitableviewController拖動到detailviewController。爲segue分配一個標識符。

而在您的自定義類中 - 爲方法tableView:didSelectRowAtIndexPath: - 調用方法performSegueWithIdentifier:並傳遞標識符。 還記得取消選擇該單元格。

在這一點上,Segue的準備將被調用。

+0

我在故事板中有一個連接。請參閱最新的問題。 – cfischer 2014-09-30 15:11:11

+0

您是否嘗試調用performSegueWithIdentifier:? – VaibhavAggarwal 2014-09-30 15:15:46

+0

我認爲這是沒有必要的,連接單元格與故事板中的下一個VC。 – cfischer 2014-09-30 15:16:47

相關問題