Search code examples
iphoneuitableviewselectedindex

table view cell selected state changes when goes to another viewcontroller?


i have a custom UITableView Cell, i have selected a particular cell and goes to another ViewController, when i came back to first view controller, selected state of the cell is not visible. how will i unchanged the selected state of a cell after navigating from another viewControllers?

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
   return [timeSet count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   timeSettingCell *newCell = nil;
   newCell = [tableView dequeueReusableCellWithIdentifier:identifier];
   if(newCell == nil)
   {
    NSLog(@"newCell ===================");
    NSArray *nibViews = [[NSBundle mainBundle] loadNibNamed:@"timeSettingCell"   owner:self options:nil];
    newCell  = [ nibViews lastObject];
    }
    newCell.timeLabel.text=[timeSet objectAtIndex:indexPath.row];

    if (newCell.selected==YES) {
      newCell.highlighted=YES;
      newCell.timeImage.image=[UIImage imageNamed:@"radioSelected.png"];
    }
    else {
      newCell.highlighted=NO;
      newCell.timeImage.image=[UIImage imageNamed:@"radioNotSelected.png"];
    }
return newCell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
value=[[time1 objectAtIndex:indexPath.row]integerValue];

SettingsViewController *settings=[[SettingsViewController alloc]initWithNibName:nil bundle:nil andCounterValue:value];
[[self presentingViewController] dismissModalViewControllerAnimated:YES];
index=indexPath.row;
[settings release];
}
-(void)viewWillAppear:(BOOL)animated{
}

thank you


Solution

  • I think you should memorize your cell state (selected or not) using a NSArray created as a singleton, ivar or NSUserDefault and then read its state in cellForRowAtIndexPath.

    EDIT: Sample code

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
    // Do what you want
    
    // This save the index of your selected cell on disk
    [[NSUserDefaults standardUserDefaults] setInteger:indexPath.row forKey:@"selectedCell"];
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    // Create your cell
    
    // Here you check if the cell should be selected or not
    if (indexPath.row == [[NSUserDefaults standardUserDefaults] integerForKey:@"selectedCell"]) {
        newCell.highlighted=YES;
        newCell.timeImage.image=[UIImage imageNamed:@"radioSelected.png"];
    } else {
        newCell.highlighted=NO;
        newCell.timeImage.image=[UIImage imageNamed:@"radioNotSelected.png"];
    }
    
    return aCell;
    }
    

    NSUserDefaults is useful to save data on your device and retrieve them later. This mean you can check your cell status even after closing and reopening your app.

    To use NSUserDefaults you need to add a Settings.bundle to you project. Take a look at NSUserDefaults Class Reference