2011-12-19 63 views
2

我有一個TableViewController x行。當用戶點擊一行時,我想傳遞給第二個tableViewController一個變量,它將決定第二個tableViewController應該加載哪些數據。我會怎麼做?如何將變量從tableview傳遞給第二個tableview?

我使用的Xcode 4.2,故事情節 下面的代碼:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *__strong)indexPath 
{ 
    // Navigation logic may go here. Create and push another view controller. 

    TestTable *detail = [self.storyboard instantiateViewControllerWithIdentifier:@"TestTable"]; 

    [self.navigationController pushViewController:detail animated: YES]; 

    detail.num = [NSString stringWithFormat:[heads objectAtIndex:indexPath.row]]; 

在「頭」我知道了一些我所要傳遞給第二tableview中,根據其第二tableview中顯示的數據。不知道最後一行是否在「num」中加載「正面」。我的方法是否正確? 感謝任何幫助/參考

+0

是什麼頭?也讓你的問題更清楚。 – Sarah 2011-12-19 10:05:07

+0

頭是一個可變數組(僅包含整數) – jason 2011-12-19 10:30:16

+0

cell.textLabel.text = [heads objectAtIndex:indexPath.row]; – jason 2011-12-19 10:31:18

回答

1
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *__strong)indexPath { 
    // Navigation logic may go here. Create and push another view controller. 

    TestTable *detail = [self.storyboard instantiateViewControllerWithIdentifier:@"TestTable"]; 

    detail.num = [[NSString stringWithFormat:[heads objectAtIndex:indexPath.row]] intValue]; 
    [self.navigationController pushViewController:detail animated: YES]; 
} 
+0

我的代碼中的錯誤是我必須在推送命令之前傳遞變量,我沒有。另請注意我在新代碼中使用的intValue。 – jason 2011-12-28 09:27:20

2

爲您的TestTable提供您想要過去的變量的屬性,例如,

@property (retain, nonatomic) NSNumber *myNumber; 
在didSelectRowAtIndexPath方法

分配給它想:

TestTable *detail = [self.storyboard instantiateViewControllerWithIdentifier:@"TestTable"]; 

// this assumes that you only have one section in your table 
detail.myNumber = [NSNumber numberWithDouble:[heads objectAtIndex:indexPath.row]]; 

[self.navigationController pushViewController:detail animated: YES]; 

detail.num = [NSString stringWithFormat:[heads objectAtIndex:indexPath.row]]; 

在你的第二個表的視圖控制器,你可以使用數字如下:

double d = [self.myNumber doubleValue]; 
+0

ok.how我可以得到它在下一個tableview? – jason 2011-12-19 10:44:15

+0

編輯答案以將用途包括在目標表中。 – Marko 2011-12-19 14:19:37

相關問題