2014-02-06 22 views
1

所以我有一個名爲cellNumString的字符串,我在我的DetailViewController的.h中創建。在我的HistoryTableViewController.m中,我有一個prepareForSegue方法,其中我將該字符串設置爲任何內容。現在我有這樣的:如何在我的prepareForSegue方法中獲取cell.textlabel.text的值?

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { 

    if ([[segue identifier] isEqualToString:@"Push"]) { 

     HistoryDetailsViewController *detailVC = (HistoryDetailsViewController*)segue.destinationViewController; 
     [detailVC setCellNumString:@" cell number"]; 
    } 
} 

我想要的是@「單元格編號」部分改爲cell.textlabel.text。但我不能使用細胞。我將如何獲得cell.textlabel.text的值並將其用作我傳遞給下一個視圖的字符串?

所有幫助表示讚賞,謝謝。

+1

通在該小區作爲發件人在您的來電performSegue – mbehan

回答

3

如果SEGUE是對細胞,你可以使用這個答案https://stackoverflow.com/a/13989915/1835155

你可以得到使用也得到了選定的單元格:

NSIndexPath *selectedIndexPath = [self.tableView indexPathForSelectedRow]; 
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:selectedIndexPath]; 
NSLog(@"%@", cell.textLabel.text); 
+0

謝謝謝謝謝謝謝謝!!!!你不知道我一直在狩獵多久才能完成這一切!我可以在1分鐘內接受你的答案! – doc92606

+0

等待我有另一個快速問題。我能夠獲得cell.textlabel.text,但現在我想知道如何在prepareforSegue方法中獲得兩個不同的部分? – doc92606

+0

啊不用介意它只是如果(indexPath.section == 0){} – doc92606

1

假設你想將字符串設定爲所選單元格或你知道你要訪問你可以做以下的,其文本標籤的單元格:

//If you want the selected cell  
NSIndexPath *path = [self.tableView indexPathForSelectedRow]; 
//If you know the cell row index e.g. 1, 2, 3... 
NSIndexPath *path = [NSIndexPath indexPathWithIndex:yourRow]; 
//Create the cell at the index path 
UITableViewCell *cell = [tableView cellForRowAtIndexPath:path] 
//Do stuff with cell... 
1

傳遞選定單元格作爲發件人參數performSegueWithIdentifier:sender:,像這樣:

[self performSegueWithIdentifier:@"showPromoViewController" sender:cell]; 

然後在prepareForSegue:sender:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { 

    if ([[segue identifier] isEqualToString:@"Push"]) { 

     HistoryDetailsViewController *detailVC = (HistoryDetailsViewController*)segue.destinationViewController; 

     // Get the value of cell.textlabel.text 
     UITableViewCell *cell = (UITableViewCell *)sender; 
     NSString *text = cell.textLabel.text; 

     // Pass it to the destination view controller 
     [detailVC setCellNumString:text]; 
    } 
} 
相關問題