Search code examples
ios7loadxcode5savensmutablearray

Can't import NSMutableArray to another class, and save tablecell objects


I have an app that is tableview/pickerview controlled and for each table i have filled with textcellobjects.

Now when customer chooses one row and continue to next tableview I want to save that object in an NSMutableArray that is allocated/initialized in one class and I want to have that class file public so objects can be added from all the different tableview classes into the NSMutableArray.

The first 2 files MotorTVController code is working well, when a tableview cell is clicked the object is added to _myTrimArray, but I want to be able to save next tablecell object in the same _myTrimArray, i try to import the TrimArray.h(2 files last in the code here) with the same save function to MotorTVController to use the savearraycode from it, but is is not working.

So I want TrimArray.h/TrimArray.h to be able to save from all the different classes with tableviews and tableviewcell text values into one NSMutableArray.

I have been struggling with this for almost a week now, I hope someone can give me a solution, I'm kinda new at this, I know this cant be so difficult to solve, but I just don't know how?

   //--------- MotorTVController.h

#import "YearPickerTVControllerPolaris.h"
#import "MotorTVController.h"
#import "motorTVCell.h"
#import "brandsTVCell.h"
#import "TrimArray.h"

@interface MotorTVController : YearPickerTVControllerPolaris

@property (nonatomic, strong) NSArray *motorLabelArray;
@property (nonatomic, strong) NSMutableArray *myTrimArray;
@end

//--------- MotorTVController.m file:

#import "MotorTVController.h"
#import "motorTVCell.h"
#import "YearPickerTVControllerPolaris.h"
#import "BackButton.h"
#import "TrimArray.h"

@interface MotorTVController ()
@end
@implementation MotorTVController
@synthesize myTrimArray = _myTrimArray;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

// Getter

- (NSMutableArray *)myTrimArray
{
    if (_myTrimArray == nil) {
        _myTrimArray = [[NSMutableArray alloc] init];
    }
    return _myTrimArray;
}

- (void)viewDidLoad
{
    UIImage *arrow = [UIImage imageNamed:@"backbutton"];
    UIButton *arrowButton = [[UIButton alloc] initWithFrame:CGRectMake(-12, 0, 36, 36)];
    [arrowButton setBackgroundImage:arrow forState:UIControlStateNormal];
    [arrowButton addTarget:self action:@selector(back)
           forControlEvents:UIControlEventTouchUpInside];

    UIView* buttonView = [[UIView alloc] initWithFrame:CGRectMake(-12, 0, 36, 36)];
    [buttonView addSubview:arrowButton];
    UIBarButtonItem * backbutton = [[UIBarButtonItem alloc] 
    initWithCustomView:buttonView];
    [self.navigationItem setLeftBarButtonItem:backbutton];
    [super viewDidLoad];

    _motorLabelArray = @[@"Original EH12 (4hk)",
                         @"Honda GX160 (5.5hk)",
                         @"Subaru EX17 (6hk)",
                         @"Honda GX200 (6.5hk)",
                         @"Briggs LO206 (9hk)",
                         @"Briggs WF206 (11HK)"];
     //NSLog(@"%@", _myTrimArray);
}

-(void)back
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _motorLabelArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:

(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"motorTVCell";
    motorTVCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier
    forIndexPath:indexPath];

    // Configure the cell...
    [cell setTintColor:[UIColor blackColor]];
    [cell setAccessoryType:UITableViewCellAccessoryDetailDisclosureButton];
    [cell setAccessoryType:UITableViewCellAccessoryDetailButton];
    NSInteger row = [indexPath row];
    cell.motorCellLabel.text = _motorLabelArray[row];
    return cell;
}

    // Which row is selected
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:
(NSIndexPath *)indexPath
{
    NSInteger rowNumber = indexPath.row;

    if (rowNumber == 0)
    {
        [_myTrimArray addObject:[_motorLabelArray objectAtIndex:0]];
        NSLog(@"You choosed %@", [_myTrimArray objectAtIndex:0]);
    }
    else if (rowNumber == 1)
    {
        [_myTrimArray addObject:[_motorLabelArray objectAtIndex:1]];
        NSLog(@"You choosed %@", [_myTrimArray objectAtIndex:1]);
    }
    else if (rowNumber == 2)
    {
        [_myTrimArray addObject:[_motorLabelArray objectAtIndex:2]];
        NSLog(@"You choosed %@", [_myTrimArray objectAtIndex:2]);
    }
    else if (rowNumber == 3)
    {
        [_myTrimArray addObject:[_motorLabelArray objectAtIndex:3]];
        NSLog(@"You choosed %@", [_myTrimArray objectAtIndex:3]);
    }
    else if (rowNumber == 4)
    {
        [_myTrimArray addObject:[_motorLabelArray objectAtIndex:4]];
        NSLog(@"You choosed %@", [_myTrimArray objectAtIndex:4]);
    }
    else if (rowNumber == 5)
    {
        [_myTrimArray addObject:[_motorLabelArray objectAtIndex:5]];
        NSLog(@"You choosed %@", [_myTrimArray objectAtIndex:5]);
    }
}
@end




        //-------------- TrimArray.m

#import "TrimArray.h"
#import "MotorTVController.h"

@interface TrimArray ()

@end

@implementation TrimArray
@synthesize myTrimArray = _myTrimArray;

// Getter

- (NSMutableArray *)myTrimArray
{
    if (_myTrimArray == nil) {
        _myTrimArray = [[NSMutableArray alloc] initWithCapacity:10];
    }
    return _myTrimArray;
}

@end


//----------------- TrimArray.h:

#import "MotorTVController.h"

@interface TrimArray : NSMutableArray

@property (nonatomic, strong) NSMutableArray *myTrimArray;

@end

Solution

  • If I understood right your question , you want an arrayObject to be available globally for all the other ViewControllers right ?

    If so ,your subclassing form NSMutableArray and give it Property of NSMutableArray is Wrong.

    To achieve this , the simplest solution would be to use NSUserDefaults.

    To sore the ArrayObject Like below. // Get the standardUserDefaults object, store your UITableView data array against a key, synchronize the defaults

    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    [userDefaults setObject:_trimArray forKey:@"TrimArray"];
    [userDefaults synchronize];
    

    To retrieve // you can retrieve the arrayObject from anywhere in your app.

    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    NSArray *arrayOfObjects = [userDefaults objectForKey:@"TrimArray"];
    // Use 'yourArray' to repopulate your UITableView