2016-04-21 223 views
1

我想創建一個segue,以便在我的項目中的tableviewcontroller和視圖控制器之間建立鏈接。但是,當我運行這些應用程序時,當我單擊表格單元格時它沒有任何反應。無法將數據從tableview傳遞到視圖控制器

這裏是我的代碼:

#import "TableViewController.h" 
    #import "SimpleTableCell.h" 
    #import "ViewController2.h" 

    @interface TableViewController() 

    @end 

    @implementation TableViewController 
    { 
     NSArray *tableData; 
     NSArray *thumbnails; 
     NSArray *detail; 


    } 

    @synthesize tableview; 

    - (void)viewDidLoad { 
     [super viewDidLoad]; 

     NSString*path = [[NSBundle mainBundle]pathForResource:@"LawFirm"ofType:@"plist"]; 

     NSDictionary*dict= [[NSDictionary alloc] initWithContentsOfFile:path]; 
     tableData=[dict objectForKey:@"Name"]; 
     thumbnails=[dict objectForKey:@"Thumbnail"]; 
     detail=[dict objectForKey:@"Detail"]; 

     [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone]; 

     UIEdgeInsets inset = UIEdgeInsetsMake(5, 5, 5, 5); 
     self.tableView.contentInset = inset; 


    } 


    #pragma mark - Table view data source 

    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 

     return 1; 
    } 

    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation 
    { 
     return (toInterfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); 

    } 

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

     return [tableData count]; 
    } 

    - (UIImage *)cellBackgroundForRowAtIndexPath:(NSIndexPath *)indexPath 
    { 
     NSInteger rowCount = [self tableView:[self tableView] numberOfRowsInSection:0]; 
     NSInteger rowIndex = indexPath.row; 
     UIImage *background = nil; 

     if (rowIndex == 0) { 
      background = [UIImage imageNamed:@"cell_top.png"]; 
     } else if (rowIndex == rowCount - 1) { 
      background = [UIImage imageNamed:@"cell_bottom.png"]; 
     } else { 
      background = [UIImage imageNamed:@"cell_middle.png"]; 
     } 

     return background; 
    } 




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

     static NSString *simpleTableIdentifier = @"SimpleTableItem"; 

     SimpleTableCell *cell = (SimpleTableCell*) [tableView dequeueReusableCellWithIdentifier: simpleTableIdentifier]; 

     if (cell == nil) { 

      NSArray*nib = [[NSBundle mainBundle]loadNibNamed:@"SimpleTableCell" owner:self options:nil]; 
      cell = [nib objectAtIndex:0]; 
      cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 
     } 

     cell.name.text = [tableData objectAtIndex:indexPath.row]; 
     cell.photo.image = [UIImage imageNamed:[thumbnails objectAtIndex:indexPath.row]]; 
     cell.detail.text = [detail objectAtIndex:indexPath.row]; 

     UIImage*background = [self cellBackgroundForRowAtIndexPath:indexPath]; 

     UIImageView *cellBackgroundView = [[UIImageView alloc] initWithImage:background]; 
     cellBackgroundView.image = background; 
     cell.backgroundView = cellBackgroundView; 

     return cell; 
    } 




    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
    { 
     return 78; 
    } 



    -(void)prepareForSegue:(UIStoryboardSegue*)segue sender:(id)sender { 
     if([segue.identifier isEqualToString:@"showlawfirmdetail"]){ 
      NSIndexPath*indexPath = [self.tableView indexPathForSelectedRow]; 
      ViewController2*vc = segue.destinationViewController; 
      vc.lawfirmname = [thumbnails objectAtIndex:indexPath.row]; 
     } 
    } 

    - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { 
     return YES; 
    } 

@end 

我已經加入導航控制器和實現代碼如下控制器之間增加SEGUE查看控制器。

任何人都可以幫助我嗎?

回答

1

當電池被竊聽

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
    [self performSegueWithIdentifier:@"showlawfirmdetail" sender:self]; 
} 
+0

謝謝,但它似乎是:對「TableViewController」不可見@interface聲明選擇「performSegueWithIdentifier:」 –

+0

我已經編輯我的答案,發送者失蹤:) – KIDdAe

0

你應該觸發SEGUE觸發後像@KIDdAe的SEGUE說,你必須從導航控制器這樣的視圖控制器:

-(void)prepareForSegue:(UIStoryboardSegue*)segue sender:(id)sender { 
    if([segue.identifier isEqualToString:@"showlawfirmdetail"]){ 
     NSIndexPath*indexPath = [self.tableView indexPathForSelectedRow]; 
     ViewController2*vc = ((UINavigationController *) segue.destinationViewController).topViewController; 
     vc.lawfirmname = [thumbnails objectAtIndex:indexPath.row]; 
    } 
} 
+0

感謝 但它會出現一個錯誤「主題1:斷點2.1''的代碼「ViewController2 * vc =((UINavigationController *)segue.destinationViewController).topViewController;」 –

0

通過將發件人傳遞給indexPath從表格視圖委託呼叫觸發segue

  • (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {self_sethWithIdentifier:@「showlawfirmdetail」sender:indexPath];

}

然後使用該實例以獲得從數據源陣列數據爲:

  • (無效)prepareForSegue:(UIStoryboardSegue *)賽格瑞發件人:(ID)發送方{ 的NSLog (@「prepareForSegue:%@」,segue.identifier);

    如果([segue.identifier isEqualToString:@ 「TeamMembersSegue」]){

    NSIndexPath * indexPath =(NSIndexPath *)發送者; ViewController2 * vc =((UINavigationController *)segue.destinationViewController).topViewController; vc.lawfirmname = [縮略圖objectAtIndex:indexPath.row];

    }

}

希望這項工作!

+0

它現在正在工作,百萬感謝您的幫助 –

+0

太好了,標記爲答案然後它會幫助其他。 – kaushal

相關問題