2011-03-10 63 views
0

我有2個視圖,都是表視圖 第一視圖 列表存在,所以當用戶點擊特定的單元格時,我想採取選定單元格的文本和存儲進入變量並按下第二個視圖如何從表中的單元格文本到字符串

例如stringVariable = cell.text

第二視圖

現在我想通過使用stringVariable可變

例如設置的視圖標題self.title = stringVariable;

我希望有人知道這個問題

預先感謝您

回答

6
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
    // Save text of the selected cell: 
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 
    stringVariable = cell.textLabel.text; 

    // load next view and set title: 
    MyView *nextView = [[MyView alloc] initWithNibName:@"MyView" bundle:nil]; 
    nextView.title = stringVariable; 
    [self.navigationController pushViewController:nextView animated:YES];   

} 
+0

非常感謝你曼尼 – Pooja 2011-03-10 13:58:30

1

您必須使用一些陣列的數據來填充表視圖。

例如。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
    } 
    TableItem * tableItem = [tableItems objectAtIndex:indexPath.row]; 
    UILabel mainLabel = [[UILable alloc] initWithFrame:()]; 
    mainLabel.text = tableItem.name; 
    [cell.contentView addSubView:mainLabel]; 
    [mainLabel release]; 

    return cell; 
} 

同樣,您應該使用相同的陣列來獲得所需的項目,選擇它的

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
     TableItem * tableItem = [tableItems objectAtIndex:indexPath.row]; 
     OtherViewController * otherViewController = [[OtherViewController alloc] init]; 
     otherViewController.name = tableItem.name; 
     [self.navigationController pushViewController:otherViewController animated:YES]; 
     [otherViewController release]; 
} 
相關問題