0

Before I post the question itself, I need to state this is a jailbreak app. This is why I'm writing in "bizarre" folders in the filesystem.

Let's continue.

Here is my cellForRowAtIndexPath method:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *MyIdentifier = @"pluginCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault  reuseIdentifier:MyIdentifier];
    }
    if(indexPath.row == 0)
    {
        cell.textLabel.text = @"default";
    }else
    {
        //Get the plugin's display name.
        NSBundle *currentPlugin = [[NSBundle alloc] initWithPath:[NSString stringWithFormat:@"/Library/Cydeswitch/plugins/%@", [plugins objectAtIndex:indexPath.row - 1], nil]];
        cell.textLabel.text = [[currentPlugin localizedInfoDictionary] objectForKey:@"CFBundleDisplayName"];
        if(cell.textLabel.text == nil)
        {
            //No localized bundle, so let's get the global display name...
            cell.textLabel.text = [[currentPlugin infoDictionary] objectForKey:@"CFBundleDisplayName"];
        } 
        [currentPlugin release];
    }

    if([[[cell textLabel] text] isEqualToString:[settings objectForKey:@"pluginToExecute"]])
    {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
        currentCell = [cell retain];
    }
    return cell;
}

Like you can see, this method uses a member called currentCell to point to the cell that is currently "selected". This is an options table and the user should be able to have only one cell with the Checkmark accessory icon at any time.

When the use selects another cell, he is changing an option and the Checkmark is supposed to disappear from the current cell and appear in the newly appeared cell. I do that like this:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    currentCell.accessoryType = UITableViewCellAccessoryNone;
    [currentCell release];
    currentCell = [[self tableView:tableView cellForRowAtIndexPath:indexPath] retain];
    NSLog(@"CURRENT CELL %@", currentCell.textLabel.text);
    currentCell.accessoryType = UITableViewCellAccessoryCheckmark;
}

But it doesn't work. The moment I tap another cell, the Checkmark correctly disappears from the old cell, but it never shows up in the new cell.

I know the selection work fine because that NSLog there prints the new cell's text just fine.

I have tried keeping track of the indexPath before, but it didn't work at all. When I tried using indexPaths instead of pointers to cells, when the user tapped the cell nothing happened at all (at least with my current approach the checkmark disappears from the old cell).

I think it has something to do with cellForRowAtIndexPath because if I keep pointing at the cells the checkmark disappears, but for some reason when trying to change the accessory type from a cell fetched with cellForRowAtIndexPath it doesn't seem to work at all.

Any help will be appreciated.

Andy Ibanez
  • 12,104
  • 9
  • 65
  • 100

2 Answers2

2

Typo? Try this:

currentCell = [[self.tableView cellForRowAtIndexPath:indexPath] retain];
Marcus Adams
  • 53,009
  • 9
  • 91
  • 143
1

You mustn't keep track of the last selected cell the way you are. Cell's get reused. Use an ivar to keep track of the indexPath or some other key appropriate to your data.

Then in the didSelect... method you get a reference to the old cell using the saved indexPath or key. In the cellForRow... method you need to set the proper accessoryType based on whether the current indexPath matches your saved indexPath.

Lastly, do not call your own delegate/data source method. When getting a reference to a cell, ask the table view for it directly.

BTW - you are over-retaining currentCell in your cellForRow... method. There is no need to retain it all in that method unless it is the first time you are making the assignment.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • Thanks. I think the solution is this along with the syntax from the answer above. Because I was indeed keeping track of the last index path but the result was being the same. I shall try. Thanks. – Andy Ibanez May 08 '13 at 15:06