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

时间:2016-04-21 16:27:27

标签: ios objective-c iphone uitableview

我想创建一个segue,以便在我的项目中的tableviewcontroller和视图控制器之间进行链接。但是,当我运行应用程序时,单击表格单元格时没有任何反应。

这是我的代码:

    #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

我添加了导航控制器,并在tableview控制器之间添加了segue来查看控制器。

任何人都可以帮助我吗?

3 个答案:

答案 0 :(得分:1)

点击单元格时应触发segue

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

答案 1 :(得分:0)

触发像@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];
    }
}

答案 2 :(得分:0)

通过将sender传递给indexPath

,从表视图委托调用中触发segue
  • (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    [self performSegueWithIdentifier:@“showlawfirmdetail”sender:indexPath];

}

然后使用此实例从数据源数组中获取数据:

  • (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { NSLog(@“prepareForSegue:%@”,segue.identifier);

    if([segue.identifier isEqualToString:@“TeamMembersSegue”]){

    NSIndexPath * indexPath =(NSIndexPath *)sender;     ViewController2 * vc =((UINavigationController *)segue.destinationViewController).topViewController;     vc.lawfirmname = [缩略图objectAtIndex:indexPath.row];

    }

}

希望这项工作!