tazboy
tazboy

Reputation: 1754

Saving and loading state of tableview

I have a tableview where the user can make sections of people using a slider. So each section can have any number of people. I want to save the state of that tableview and then reload it when they come back.

I figured that since I'm using core data I can give each person a row and section attribute. So I'm able to save that but I don't know the best way to use those values to fill the tableview when it reappears.

I don't think that NSUserDefaults would work the best because I have many groups that can be broken into sections. I've been struggling with the best way to do this for a few days now and I'm still not sure what way to go.

More (per mihir mehta):

// Set core data values
    int sec = 0;
    int row = 0;
    for (NSArray *section in groupsArray) {
        for (People *person in section) {
            [person setSubgroupSection:[NSNumber numberWithInt:sec]];
            [person setSubgroupRow:[NSNumber numberWithInt:row]];
            row++;
        }
        sec++;
        row = 0; // new section so restart the row count
    }

Upvotes: 0

Views: 185

Answers (3)

Mihir Mehta
Mihir Mehta

Reputation: 13833

Make a function that will take row and Section as argument and Returns that particular person by searching the array .... Got my point ?

Upvotes: 0

tacos_tacos_tacos
tacos_tacos_tacos

Reputation: 10585

If you are already familiar with CoreData then perhaps you should stick with the plan you describe. The way I see it you should make some kind of TableViewInfoManagedObject:NSManagedObject. This TableViewInfoManagedObject should have members like @dynamic numberOfSections for example that describe what you need for your table view to work.

If you use CoreData to manage the people already consider using relationships to map numberOfSections to numberOfGroups or whatever you have in your People:NSManagedObject.

Also you need to consider when the appropriate time to "save" your state, which seems to be completely determined by the slider. In that case you may want to implement an IBAction for valueChanged.

EDIT 0: Based on the snippet you have provided it seems like at the end of the loop you would have the requisite info you need. The final value of sec should correspond to the UITableViewDataSource delegate method -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView and I am not really sure why you are setting the row number of the People object unless you are trying to achieve some sort order, which should be accomplished anyway by an NSSortDescriptor. So tableView:numberOfRowsInSection should return something like [[peopleinSection: section] count] and your tableView:cellForRowAtIndexPath should be set up so that it returns a cell like cell.textLabel.text = [[[peopleInSection:indexPath.section] objectAtIndex:indexPath.row] getPersonName]. Makes sense?

Upvotes: 2

Dylan Reich
Dylan Reich

Reputation: 1430

How about creating a class (subclass of NSObject) for each object that you need to save, and in that class you can add properties for each object. Then, you can use NSKeyedArchiver/Unarchiver to save and load your objects back to reuse.

Upvotes: 1

Related Questions