Search code examples
iosuitableviewuikit-state-preservation

how to retain TableViewCell colour while toggling controllers in iOS


I have created StoryBoard segue application containing 1st CategoryViewController and 2nd CheckListTableController. Category(buttons) wise I am opening different question list on tableview of CheckListTableController.

I have set check and uncheck button and its respective colour in tableView cell. I have taken NSMutableArray and adding indexPath to this to set the particular colour to cell.

When I am navigating between these controller I want my tableView cell colour should be reserve.

Any Idea how to do this. Thanks in advance.

Edited with query 26Aug13

Now I need same functionality by using plist file or Core data. So that user can save sessions of task and access it later. Eg. in session one he did check uncheck mark for ABC building then session saved. Session two again he did check/unchk activity for different location XYZ building then saved.

Which method would be suitable here. Plist file or Core data? Can you redirect me to specific solution.

Edit with some Code

Catergory_ViewController.m 

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender

{
if([segue.identifier isEqualToString:@"chkListTable"])
{
     NSString *bldgArrayString = [[NSString alloc] initWithFormat:@"bldg"];
    checkListTableViewController *chk= segue.destinationViewController;

    chk.gotquestArrayString = bldgArrayString;   

}
}



checkListTableViewController.m 


- (void)viewDidLoad
{
[super viewDidLoad];
if ([gotquestArrayString isEqual:@"bldg"])
{

    buildingQuest = [[NSMutableArray alloc]initWithObjects:@"Motor vehicles are not  parked in the space",

                     @"Lightning protection system (Electronic)",

                     @"Lightning protection systems available on all buildings",

                     @"Reception facilities in order and not damaged",
}
}
 //tableView button method where indexPath added to MutableArray
-(void)yesAction:(id)sender
{
arrayCheckUnchek = [[NSMutableArray alloc]init];
UIButton *button = (UIButton *)sender;
UITableViewCell *cell = (UITableViewCell *)button.superview.superview;
UITableView *curTableView = (UITableView *)cell.superview;
NSIndexPath *indexPath = [curTableView indexPathForCell:cell];
cell.backgroundColor = [UIColor greenColor];
[arrayCheckUnchek addObject:indexPath];
}

- (void)tableView: (UITableView*)tableView
 willDisplayCell: (UITableViewCell*)cell
forRowAtIndexPath: (NSIndexPath*)indexPath
{
// UITableViewCell *Cell = [myChklist cellForRowAtIndexPath:indexPath];
 if ([arrayCheckUnchek containsObject:indexPath])
 {
    cell.backgroundColor = [UIColor greenColor];
 }
 }

Solution

  • 1.you need to save your NSMutable Array in Plist file or in UserDefaluts whenever you have changed the selection(checked/ unchecked) at that time you should update your Plist or userdefaluts 2.Read your plist file in ViewWillApper method in your tableviewcontroller. and pass this data to your "arraycheckuncheck"

    hope it will work for you

    happy icoding ....

    • NSAnant