Ios 从UITableViewCell向NSArray添加对象

Ios 从UITableViewCell向NSArray添加对象,ios,objective-c,uitableview,parse-platform,tableviewcell,Ios,Objective C,Uitableview,Parse Platform,Tableviewcell,我创建了一个自定义的UITableViewCell,其中包含一个PFObject属性和一个按钮。我希望按钮将此对象添加到NSMutableArray上,并将此数组传递给另一个UIViewController。问题是我无法将prepareforsgue方法实现到自定义UITableViewCell中,因此当我在rescardconfimationvc中显示数组时,我总是得到一个空数组 这是我的代码: #import "boxTableViewCell.h" #import "RestoCardCo

我创建了一个自定义的
UITableViewCell
,其中包含一个
PFObject
属性和一个按钮。我希望按钮将此对象添加到
NSMutableArray
上,并将此数组传递给另一个
UIViewController
。问题是我无法将
prepareforsgue
方法实现到自定义
UITableViewCell
中,因此当我在
rescardconfimationvc
中显示数组时,我总是得到一个空数组

这是我的代码:

#import "boxTableViewCell.h"
#import "RestoCardConfirmationViewController.h"
#import "RestauCardViewController.h"

@implementation boxTableViewCell {
   NSMutableArray *_pickerPlace;
   RestauCardViewController *_restoCardVC;
   RestoCardConfirmationViewController *_restoCardConfirmationVC;
}

- (void)awakeFromNib {
   // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
   [super setSelected:selected animated:animated];

   // Configure the view for the selected state
}


- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
   return 1;
}


- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
   return _pickerPlace.count;
}

- (NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    return [_pickerPlace objectAtIndex:row];
}

- (IBAction)select:(id)sender {
   NSLog(@"the select box is : %@",_box);
   [_restoCardConfirmationVC.boxesCommande addObject:_box];
   self.select.enabled = NO;
}
@end
再次确认
#导入
#进口
@接口重新装入配置ViewController:UIViewController
@属性(非原子)PFObject*命令;
@属性(非原子)NSMutableArray*BoxesCommand;
-(iAction)确认人:(id)发送方;
@结束

您应该在使用阵列之前对其进行初始化。像这样

- (void)awakeFromNib {
   // Initialization code
   _restoCardConfirmationVC = [[RestoCardConfirmationViewController alloc] init];
   _restoCardConfirmationVC.boxesCommande = [NSMutableArray array];
}

尽管这是一个糟糕的方法。您应该在ResCardConfigurationViewController初始化中初始化数组。

您尚未初始化要在其中添加对象的数组。在
awakeFromNib
ViewDidLoad
中分配它,然后在其中添加一个对象

此外,我没有找到在TableViewCell的awakeFromNib中分配视图控制器的好理由。您应该只在点击按钮时分配VC

因此,代码应类似于:

- (IBAction)select:(id)sender {

    _restoCardConfirmationVC = [[RestoCardConfirmationViewController alloc] init];

    [_restoCardConfirmationVC view]; //This will call the ViewDidLoad in Advance

    [_restoCardConfirmationVC.boxesCommande addObject:_box];

   self.select.enabled = NO;
}
在ResTocardConfiguration ViewController的ViewDidLoad中:

- (void)viewDidLoad {

    [super viewDidLoad];

    _boxesCommande =  [NSMutableArray array];

    //Other initialisation code
}

我初始化了它们,但当我试图显示boxesCommande数组时,我总是在RestCardConfiguration VC中得到空数组。我已经在RestCardConfiguration VC中创建了一个NSMutableArray属性:@property(非原子)NSMutableArray*boxesCommande;您可能推送/显示了错误的视图控制器。我这样做了,但在ResTocardConfiguration视图中,我总是得到一个空数组Controller@aissaelouafi:您在哪里分配ResTocardConfigurationViewController?更新您的代码我已在awakFromNib方法中分配了restardconfimationviewcontroller:-(void)awakeFromNib{u restardconfimationvc=[[restardconfimationviewcontroller alloc]init];\u restardconfimationvc.boxesCommande=[NSMutableArray];}调用[\u restardconfimationvc view]分配后,看看它是否解决了你的问题?我尝试过,但我总是得到相同的问题。我不应该在ResTocardConfiguration VC中实例化BoxesCommand数组?那是什么“另一个视图控制器”?当在TableViewController中选择一行时,您是否分配并显示它?@JamalZafar我已经创建了一个带有按钮的自定义ViewTableCell,我想向数组中添加一个PFObject
- (void)viewDidLoad {

    [super viewDidLoad];

    _boxesCommande =  [NSMutableArray array];

    //Other initialisation code
}