Frage

There are many questions on this topic and I've been trying this for hours with the various options, but I'm in need of some real assistance with this.

The main premise is: I have a Tab Bar controller with two tabs; the first is a Timeline and the second is the In-App Settings (both table view controllers). In the In-App settings, when I select "Keyboard", I'm taken to another Table view by segue with two static cells: Light and Dark.

What I want to achieve is: When I tap on one of these cells, it puts a checkmark accessory view on that cell.

Using NSUserDefaults, I want to maintain the selected cell and set that cells' accessoryType to checkmark on the next view reload.

I followed this questions' selected answer and while my code allows me to select either the Light or Dark cell; it never maintains that in between reloading.

Here's my cellForRowAtIndexPath:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [super tableView:tableView cellForRowAtIndexPath:indexPath];    
    self.selectedKeyboardCell = [[NSUserDefaults standardUserDefaults] objectForKey:@"Selected"];

    NSLog(@"The selected keyboard is %@", self.selectedKeyboardCell);

    if([self.checkedIndexPath isEqual:indexPath])
    {
        NSLog(@"Loaded");
        cell.accessoryType = UITableViewCellAccessoryCheckmark;

    }
    else
    {
        NSLog(@"Not Loaded");

        cell.accessoryType = UITableViewCellAccessoryNone;
        /*if ([self.selectedKeyboardCell isEqualToString:@"Light"])
        {
            NSLog(@"P");
            if (indexPath.row == 0)
            {
                NSLog(@"Q");
                cell.accessoryType = UITableViewCellAccessoryCheckmark;
            }
            cell.accessoryType = UITableViewCellAccessoryNone;

        }
        else
        {
            NSLog(@"R");
            if (indexPath.row == 1)
            {
                NSLog(@"Z");
                cell.accessoryType = UITableViewCellAccessoryCheckmark;
            }
            cell.accessoryType = UITableViewCellAccessoryNone;
*/

    }

My didSelectRowAtIndexPath is:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Uncheck the previous checked row
    if(self.checkedIndexPath)
    {
        UITableViewCell* uncheckCell = [tableView
                                        cellForRowAtIndexPath:self.checkedIndexPath];
        uncheckCell.accessoryType = UITableViewCellAccessoryNone;
    }
    UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
    self.checkedIndexPath = indexPath;

    self.selectedKeyboardCell = cell.textLabel.text;

    [[NSUserDefaults standardUserDefaults] setObject:self.selectedKeyboardCell forKey:@"Selected"];

    self.selectedKeyboardTheme = cell.textLabel.text;

    [tableView deselectRowAtIndexPath:indexPath animated:YES];


    [[NSUserDefaults standardUserDefaults] setObject:self.selectedKeyboardTheme forKey:@"Keyboard"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

I have a [self.keyboardTableView reloadData]; in my viewWillAppear.

I have commented out some code I've been trying, but no matter how many posts I read, I just cannot find the solution here.

This question talks about shifting code to the viewWillAppear but I have no idea what code he's saying: Ho to refresh view and tableview, and checkmark a default setting cell on load?

Problem: I want to select a cell in the Table View and have that cell selected with a checkmark accessory type the next time (and every time this view loads). It's a static table and the text is ONLY ever going to say Light or Dark.

In the cellForRowAtIndexPath, self.selectedKeyboardCell accurately tells me whether it's Light or Dark, depending on which cell was selected before the view was loaded. However, how do I say "If Light, set the checkmark on the FIRST cell and remove the checkmark from the second cell and if Dark, set the checkmark on the SECOND cell and remove the checkmark from the first cell".

Update: For clarity, I am able to select either the Light or Dark static Table view cell and whichever cell I select, the checkmark appears correctly. The problem is when I go to another view and come back to this view, the previously selected cell does not show. I'm saving the selectedCell's textLabel to NSUserDefaults and in the NSLog, it shows me the text of the selected cell but it does not show me the checkmark on the selected cell.

Update Two: In order to set the default selected cell to Light the first time the app was run, I did the following in the cellForRowAtIndexPath:

if (!self.selectedKeyboardCell)
{
    self.checkedIndexPath = [NSIndexPath indexPathForRow:0 inSection:0];
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
}

Any guidance on this would be seriously appreciated.

War es hilfreich?

Lösung

Honestly your code is a little confusing and it would help, if you had said a little more what actually works. But:

In the viewWillAppear you should just set the self.checkedIndexPath:

if ([self.selectedKeyboardCell isEqualToString:@"Light"])
{
   self.checkedIndexPath = [NSIndexPath indexPathForRow:0 inSection:0];
}else{
   self.checkedIndexPath = [NSIndexPath indexPathForRow:1 inSection:0];
}
[self.keyboardTableView reloadData];

right?

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top