2012-10-01 49 views
1

我正在使用storyBoard處理Iphone應用程序。將自定義UITableViewCell的屬性傳遞給prepareForSegue方法中的目標視圖

我在UINavidationView中有一個UITableView。我在自定義單元格中加載數據。然後我當用戶點擊一個單元格時我轉到另一個視圖(ResultView)。

我在故事板中設置了視圖和segue。

我的目標是將數據從prepareForSegue方法傳遞給ResultView。

爲此,我創建了一個實現UITableViewCell的自定義單元。然後我添加了一個名爲creationDate的NSDate屬性。我需要將選定單元格的創建日期傳遞給ResultView。我有以下 ate = readingCell.creationDate;

- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{ 
    if([segue.identifier isEqualToString:@"resultViewSegue"]) 
    { 
     //Get a reference to the destination 
     ResultsViewController * destinationView = segue.destinationViewController; 

     //I try to get the selected cell in order to pass it's property 
     historyCellClass * selectedCell = (historyCellClass*) sender; 

     //pass the creation date to the destination view (it has its own creation date property) 
     [destinationView setCreationDate:selectedCell.creationDate]; 
    } 
} 

但是結果視圖的創建日期始終爲空。

看起來像我沒有得到所選單元格的參考以閱讀其屬性。

如何將單元格的日期傳遞給下一個視圖?

非常感謝您的幫助

回答

1

我處理這個問題的方法是用SEGUE的手動觸發和伊娃表示選擇狀態。

確保被觸發的segue從一個視圖控制器轉到下一個(而不是從一個tableView單元格)。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
    self.selectedModel = [self.myModel objectAtIndex:indexPath:row]; 
    [self performSegueWithIdentifier:@"resultsViewSegue"]; 

selectedModel是新的ivar,其類型是相同的支撐表數據源陣列中的單個元件。請按照您在cellForRowAtIndexPath中的指引路徑查找它:

現在prepareForSegue:..

- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{ 
    if([segue.identifier isEqualToString:@"resultViewSegue"]) 
    { 
     //Get a reference to the destination 
     ResultsViewController * destinationView = segue.destinationViewController; 

     //pass the creation date to the destination view (it has its own creation date property) 
     [destinationView setCreationDate:self.selectedModel.creationDate]; 

     // selectedModel.creation date might not be right... use whatever way you get to creationDate 
     // from the indexPath in cellForRowAtIndex path, that's the code you want above. 
    } 
} 

有什麼狀態中保存表視圖選擇和SEGUE的開始之間的幾個選擇。您可以保存選定的索引路徑或模型元素(如我所建議的),或者僅保存您打算傳遞的模型的方面(例如creationDate)。表格單元本身是保存狀態唯一不好的主意。

+0

我試過這個,但它在「didSelectRowAtIndexPath」之前進入「prepareForSegue」方法。任何想法爲什麼? – Youssef

+0

對不起,我忘了提及這一點。我認爲這是一個真正的弱點 - 從細胞到下一個VC,它對我來說似乎非常玩具。將編輯答案... – danh

+0

謝謝先生。它的工作就像一個魅力 – Youssef

相關問題