我有tableView
,我可以从中导航到' viewController'即PassengerInfoViewController
,其中我有一个表单。我填写表单,从中填写dictionary
并将其添加到NSMutableArray
。
所以当我回到带有填充数组的tableView' I would like to pass this array and then reload the
tableView`时。
所以,我正在做的事情: -
//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];
一旦我向后导航,我会重新加载表格视图。但是celForRow
方法不会填充新值。
-(void)getPassengerinfo:(NSMutableArray*)arr{
passenger_infoArray =[[NSMutableArray alloc]init];
passenger_infoArray = arr;
NSLog(@"Passenger Info Array : %@", passenger_infoArray);//Shows array of dictionaries
[passengerInfoTableView reloadData];
}
我的cellForRow
方法如下所示:
- (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;
}
答案 0 :(得分:1)
您可以使用
传递数据使用代理是很好的做法,最后两个不是首选。
答案 1 :(得分:0)
您可以使用委托方法
将数据传递给上一个视图控制器在 PassengerInfoViewController.h 中,在import
声明的上方添加以下代码
@protocol passengerInfoViewControllerdelegate <NSObject>
-(void)sendData:(NSMutableArray *)arrData;
@end
之后创建一个属性,如下所示
@property(nonatomic,assign)id delegate;
现在,在 PassengerInfoViewController.m 中合成属性,之后
-(void)viewWillDisappear:(BOOL)animated
{
[delegate sendData:yourArray];
}
现在在tableview
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
PassengerInfoViewController *pivc = [self.storyboard instantiateViewControllerWithIdentifier:@"PassengerInfoViewController"];
pivc.delegate = self; //Add this line to your code
[self.navigationController pushViewController:pivc animated:YES];
}
现在,是时候实现我们在viewcontroller中创建的委托方法
了-(void)sendData:(NSMutableArray *)arrData
{
passenger_infoArray = arrData;
[passengerInfoTableView reloadData];
}
答案 2 :(得分:0)
执行此操作的旧方法是使用委托。更好的方法是通过展开segue,因为代码少得多而且你不必创建协议。
表单完成后,您的PDC应调用[self performSegue:@"theNameOfTheUnwindSegueeYouCreateInTheStoryboard"]
。然后,在PDC的prepareForSegue
方法中,从destinationViewController
参数获取storyboardSegue
属性 - 这是包含表视图的VC。设置你用来支持表格视图的任何模型属性 - 在你的情况下,它看起来像是passenger_infoArray
对象,如果不是这个对象你必须变成可变的,如果它不是,那么它也是公开的。已经。