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
}
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. ThisTableViewInfoManagedObjectshould have members like@dynamic numberOfSectionsfor 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
numberOfSectionstonumberOfGroupsor whatever you have in yourPeople: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
IBActionfor 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
secshould correspond to theUITableViewDataSourcedelegate method-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableViewand I am not really sure why you are setting the row number of thePeopleobject unless you are trying to achieve some sort order, which should be accomplished anyway by anNSSortDescriptor. SotableView:numberOfRowsInSectionshould return something like[[peopleinSection: section] count]and yourtableView:cellForRowAtIndexPathshould be set up so that it returns a cell likecell.textLabel.text = [[[peopleInSection:indexPath.section] objectAtIndex:indexPath.row] getPersonName]. Makes sense?