无法将数据从tableview传递到视图控制器

问题描述:

我想创建一个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查看控制器。

任何人都可以帮助我吗?

当电池被窃听

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

谢谢,但它似乎是:对“TableViewController”不可见@interface声明选择“performSegueWithIdentifier:” –

+0

我已经编辑我的答案,发送者失踪:) – KIDdAe

你应该触发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;” –

通过将发件人传递给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