简体   繁体   中英

unable to pass data from tableview to view controller

I would like to create a segue in order to link between tableviewcontroller and view controller in my project. However, when i run the apps, it has no reaction when i click the table cell.

Here is my code:

    #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

I have added navigation controller and added segue between tableview controller to view controller.

Any one can help me?

You should trigger the segue when the cell is tapped

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

After trigger the segue like @KIDdAe say, you must get the view controller from the navigation controller like this:

-(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];
    }
}

Trigger segue from table view delegate call by passing sender to indexPath

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

    [self performSegueWithIdentifier:@"showlawfirmdetail" sender: indexPath];

}

then use this instance to get data from datasource array as :

  • (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 = [thumbnails objectAtIndex:indexPath.row];

    }

}

Hope this work !

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM