Search code examples
iosuitableviewdata-storage

Setting Accessory on cell in viewDidLoad


I have two table view controllers, "root" and "detail". In my root view I have this cell which says "Status". I need to be able to change that. When I click it i am seguewayd to the detailed view which shows the statuses I can pick (static cells). When I click on a status, a checkmark is displayed. But this checkmark also needs to be displayed when a status is previously chosen. So the checkmark should be visible as soon as I am seguawayd in from the root view.

I am able to display a checkmark next to the status as soon as i click a cell. But when I want the checkmark to be displayed when i'm seguawayd, it doesn't show. This is the code im using

- (void) setCheckmarkOnIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];

    cell.accessoryType = UITableViewCellAccessoryCheckmark;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self setCheckmarkOnIndexPath:indexPath];
}

- (void)viewDidLoad 
{
    [super viewDidLoad];

    // indexPathForRow set to the third cell to demo
    [self setCheckmarkOnIndexPath:[NSIndexPath indexPathForRow:2 inSection:0]];
}

enter image description here Static cells for Statuses


Solution

  • You need to store the user selection somewhere, and pass it into your controller (or access it in your controller depending on where it's saved and which object owns that information).

    Currently, you don't save the user selection so you can't update the cell. Note also that you should be updating the selected cell in viewDidAppear:, not viewDidLoad, and that you should look at using tableView:willDisplayCell:forRowAtIndexPath: to properly deal with the table view being scrolled (and cells being replaced / reused).