Hamza Imran
Hamza Imran

Reputation: 189

Pass data from Table View Cell to a View Controller in IOS

I know this question is asked many time , i had searched a lot and tried many solution but not worked. I have made a customize table view in which data is load from a service. The data load is quite limited , i have to show the detail of data into new view controller when user click on a cell. Its should pass data of the respective cell which carries data. I have tried segue technique to pass data to new vc but fails , its shows null in value which i'm passing. I have created some labels in new vc in which i'm calling the values from table view cell. My code is,

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:@"ShowDetail"]) {
        //Do something
        ShowViewController *destViewController = segue.destinationViewController;
        NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];


         destViewController.tites = [_Title objectAtIndex:indexPath.row];
         destViewController.prce = [_Price objectAtIndex:indexPath.row];
         NSLog(@"PPPP is %@",destViewController.phon);
         destViewController.ara = [_LandArea objectAtIndex:indexPath.row];
         destViewController.phon = [_Phone objectAtIndex:indexPath.row];
         destViewController.citi = [_City objectAtIndex:indexPath.row];
         destViewController.loc = [_location objectAtIndex:indexPath.row];
         destViewController.tye = [_type objectAtIndex:indexPath.row];
         destViewController.Idss = [_Id objectAtIndex:indexPath.row];
         destViewController.nam = [_name objectAtIndex:indexPath.row];
         destViewController.emal = [_email objectAtIndex:indexPath.row];
         destViewController.roomss = [_rooms objectAtIndex:indexPath.row];
         destViewController.wash = [_washroom objectAtIndex:indexPath.row];
         destViewController.flloors = [_floor objectAtIndex:indexPath.row];
         destViewController.stat = [_status objectAtIndex:indexPath.row];
         destViewController.descrp = [_descrip objectAtIndex:indexPath.row];
         destViewController.countryy = [_country objectAtIndex:indexPath.row];

    }
}

Upvotes: 0

Views: 1081

Answers (3)

3stud1ant3
3stud1ant3

Reputation: 3606

Issue in this question is that you are not populating the _Price and other arrays properly, so where you are populating _Title array , fill other arrays as well like _Price, _City

Upvotes: 1

KKRocks
KKRocks

Reputation: 8322

Make following changes in your code :

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

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
 {
     if ([segue.identifier isEqualToString:@"ShowDetail"])
      {
    ShowViewController *destViewController =(ShowViewController *) segue.destinationViewController;
           NSIndexPath *indexPath = (NSIndexPath *)sender;
           destViewController.tites = [_Title objectAtIndex:indexPath.row];
           destViewController.prce = [_Price objectAtIndex:indexPath.row];
           // use this indexpath for segue
      }
  }

Upvotes: 0

luckyShubhra
luckyShubhra

Reputation: 2751

An outlet doesn't instantiate because an outlet is a variable (or property).

The objects in a nib are instantiated when that nib is loaded, and they are assigned to each outlet as immediately as possible afterward, after the objects are created but before awakeFromNib is sent to all relevant objects.

In your case you can pass data in ShowViewController and update the label in ShowViewController's viewDidLoad or viewDidAppear.

Define string in ShowViewController interface as

@property (strong, nonatomic) NSString * titesStr;

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
     if ([segue.identifier isEqualToString:@"ShowDetail"]) {
     //Do something
     ShowViewController *destViewController = segue.destinationViewController;
     NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
     destViewController.titesStr = [_Title objectAtIndex:indexPath.row];
     ....
     ....
     ....
     }
}

In ShowViewController viewDidAppear update your label as

- (void)viewDidAppear:(BOOL)animated
 {
      [super viewDidAppear:animated];
      tites.text = titesStr;
 } 

In place of passing data one by one you can use array also. Best implementation would be using model class.

EDIT

[self performSegueWithIdentifier:@"ShowDetail" sender:tableView];
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
     if ([segue.identifier isEqualToString:@"ShowDetail"]) {
     //Do something
     UITableView *tv = (UITableView*)sender;
     ShowViewController *destViewController = segue.destinationViewController;
     NSIndexPath *indexPath = [tv indexPathForSelectedRow];
     destViewController.tites = [_Title objectAtIndex:indexPath.row];
     ....
     ....
     ....
     }
}

Upvotes: 0

Related Questions