2015-02-12 64 views
0

我是iOS開發人員的全新職位。我正在構建一個基本項目,我有兩個視圖控制器。在其中一個我有一個tableView,另一個我有一個標籤和一個文本框。在tableview中,我使用了一個自定義單元格,並且已經採用了兩個標籤。在這兩個標籤中,我分配了兩個數組。現在我想將兩個數組的數據顯示到另一個視圖控制器的標籤和文本字段。所以我想知道在didSelectRowAtIndexPath方法中寫什麼代碼。如何在單擊該特定單元格時將tableview單元格的數據顯示給另一個viewcontroller?

這是我所做的代碼。

#import "TabViewController.h" 

@interface TabViewController() 

@end 

@implementation TabViewController 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view. 
    self.peoplenames = @[@"Soumen Da" ,@"Debu Da" ,@"Sandipan Da" ,@"Pradipta Da" ,@"Sandeep Da" ,@"Pran Da" ,@"Amit Da" ,@"Piyali Di" ,@"Niladri Da" ,@"Biswa Da", @"Soumitra Da" ,@"Utpal Da", @"Sandip Da"]; 

    self.peopleaddress = @[@"Santragachhi" ,@"Kolkata" ,@"Shambazar" ,@"Jadavpur" ,@"Saltlake" ,@"New Town" ,@"Beleghata" ,@"Garia" ,@"Barakpur" ,@"Naihati" ,@"Agarpara" ,@"Sodpur" ,@"Park Street"]; 
} 

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell1"]; 


    UILabel *lbl1 = (UILabel*)[cell.contentView viewWithTag:2]; 
    lbl1.text = [self.peoplenames objectAtIndex:indexPath.row]; 
    NSLog(@"row===%ld",(long)indexPath.row); 

    UILabel *lbl2 = (UILabel*)[cell.contentView viewWithTag:1]; 
    lbl2.text = [self.peopleaddress objectAtIndex:indexPath.row]; 
    NSLog(@"row===%ld",(long)indexPath.row); 

    return cell; 

} 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

    [self performSegueWithIdentifier:@"detail" sender:self]; 

} 


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ 

    return [self.peoplenames count]; 


} 

- (void)didReceiveMemoryWarning { 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

@end 

塊引用

回答

0

你可以把一個賽格瑞從表格單元格的下一個視圖控制器,無需做任何事情- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath.

使用- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender與表第一視圖控制器查看並訪問segue.destinationViewController.label和segue.destinationViewController.label.textField以將值傳遞給第二個視圖控制器。

+0

我是否需要爲第二個視圖控制器創建另一個類? – Tirthendu 2015-02-12 07:09:39

+0

是的,您需要創建 – Arjuna 2015-02-12 07:58:00

+0

非常感謝。一切都完成了。 – Tirthendu 2015-02-12 08:38:16

0

如果你想在didSelectRowAtIndexPath中做如下的操作。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
UIStoryboard *sb=[UIStoryboard storyboardWithName:@"Main" bundle:nil]; 
     DetailViewController *vc=(DetailViewController *)[sb instantiateViewControllerWithIdentifier:@"detail"]; 
     vc.label.text =[self.peopleaddress objectAtIndex:indexPath.row]; 
vc. textfield.text =[self.peopleaddress objectAtIndex:indexPath.row]; 

} 
+0

'不包含具有標識符'detail'的視圖控制器。這個錯誤即將到來。 – Tirthendu 2015-02-12 07:42:10

+0

轉到故事板中的DetailViewController和DetailViewController的goto類檢查器,您可以在StoryBoard id字段中找到'detail',然後運行。 – 2015-02-12 08:34:43

+0

但首先創建帶有標籤和文本字段的DetailViewController。 – 2015-02-12 08:35:57

相關問題