I have a tableView from which I navigate to a 'viewController' ie PassengerInfoViewController in which I have a form.I fill up the form , make a dictionary out of it and add it to a NSMutableArray.
So when I am going back to the tableView' I would like to pass this array and then reload thetableView` with the filled array.
So here's what I am doing : -
//navigate to the form
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
PassengerInfoViewController *pivc = [self.storyboard instantiateViewControllerWithIdentifier:@"PassengerInfoViewController"];
[self.navigationController pushViewController:pivc animated:YES];
}
//After filling up the form
-(void)goBackToPassengerDetails:(UIButton*)sender{
NSString *title = self.titleTextField.text;
NSString *fname = self.firstNameTextField.text;
NSString *lname =self.lastNameTextField.text;
NSString *ageStr = self.ageTextField.text;
NSMutableDictionary *dict = [[NSMutableDictionary alloc]init];
[dict setValue:title forKey:@"title"];
[dict setValue:fname forKey:@"firstName"];
[dict setValue:lname forKey:@"lastName"];
[dict setValue:ageStr forKey:@"age"];
Passenger *passengerObj = [Passenger sharedInstance]; //Singleton
[passengerObj.passengerDetailArray addObject:dict];
PassengerDetailsViewController *pdc = [self.storyboard instantiateViewControllerWithIdentifier:@"PassengerDetailsViewController"];
[pdc getPassengerinfo:passengerObj.passengerDetailArray];
[self.navigationController popViewControllerAnimated:YES];
Once I navigate back I reload the table view.However celForRow method doesn't populate new values.
-(void)getPassengerinfo:(NSMutableArray*)arr{
passenger_infoArray =[[NSMutableArray alloc]init];
passenger_infoArray = arr;
NSLog(@"Passenger Info Array : %@", passenger_infoArray);//Shows array of dictionaries
[passengerInfoTableView reloadData];
}
My cellForRow method looks like this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//Not called when I am doing reload data
static NSString *CellIdentifier = @"Cell";
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier ];
}
NSLog(@"Table view array : %@",passenger_infoArray);
if (passenger_infoArray.count>0) {
NSString *temp= [[passenger_infoArray objectAtIndex:indexPath.row]objectForKey:@"firstName"];
cell.textLabel.text = temp;
}else{
cell.textLabel.text = [NSString stringWithFormat:@"Passenger %ld", (long)indexPath.row+1];
cell.imageView.image = [UIImage imageNamed:@"user.png"];
}
return cell;
}
You can pass data to previous view controller by using delegate method
In PassengerInfoViewController.h add below code just above of your
importstatementAfter this create one property, as shown below
Now, in PassengerInfoViewController.m synthesize the property and after that
Now in
tableviewNow, it's time to implement delegate method which we created in viewcontroller