Organizational Research By

Surprising Reserch Topic

boostr.in questions - Question:Retrieving and re-saving an nsmutablearray to nsuserdefaults from a uitableview


I have a uitableview with a list of items populated from a JSON file located locally within the app. Everything works as far as getting the list to the table and the multiple selection of items which when selected (or deselected) are then saved to a nsmutablearray.

The problem is when the user leaves the view and returns and selects another item (or deselects a currently selected item). At this point the mutable array is then empty.

I'm not sure if the nsuserdefaults saving of the mutable array is the problem. it saves it fine but then when the view reappears (the mutable array's value is fine at this point) and the user touches a table row the array is null once more.

my .h file:

@interface CategoriesViewController : UITableViewController {

    NSMutableArray *_selectedItems;

    NSString *filePath;

    NSString *string;

}

// arForTable array will hold the JSON results from the api
@property (nonatomic, retain) NSArray *arForTable;

@property (nonatomic, retain) NSMutableArray *categorySelected;

@property (nonatomic, retain) NSString *jsonStringCategory;
@property(nonatomic, retain) UIView *accessoryView;

@end

my .m file:

@implementation CategoriesViewController
@synthesize arForTable = _arForTable;

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.categorySelected = [[NSMutableArray alloc] init];

    [self reloadMain];

    // assignment reference so don't release
     _selectedItems = [(AppDelegate *)[[UIApplication sharedApplication] delegate] selectedCategories];

    self.tableView.hidden = NO;

}

-(void) reloadMain {

    // countrySaved value from NSUserDefaults
    NSUserDefaults * defaults =  [NSUserDefaults standardUserDefaults];

    NSString *countryString = [defaults stringForKey:@"selectedCountryTableString"];
    NSString *cityString = [defaults stringForKey:@"selectedCityTableString"];
    NSLog(@"countrystring from category is %@", countryString);
    NSLog(@"citystring from category is %@", cityString);

    // getting path to the file

    if ([defaults stringForKey:@"selectedCountryTableString"] == NULL) {

        filePath = [[NSBundle mainBundle] pathForResource:@"categoriesit" ofType:@"json"];

    } else if ([countryString isE
asked Sep 13, 2013 in Java Interview Questions by rajesh
recategorized Sep 12, 2013 by rajesh
0 votes
20 views



Related Hot Questions



Government Jobs Opening


...