0

So i have a tableView with checkmark accessory type and I have such method:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSUInteger newRow = [lastIndexPath row];
    NSUInteger oldRow = (lastIndexPath != nil) ? [lastIndexPath row] : -1;

    if (newRow != oldRow) {
        UITableViewCell *newCell = [tableView cellForRowAtIndexPath:indexPath];
        newCell.accessoryType = UITableViewCellAccessoryCheckmark;

        UITableViewCell *oldCell = [tableView cellForRowAtIndexPath:lastIndexPath];
        oldCell.accessoryType = UITableViewCellAccessoryNone;
        lastIndexPath = indexPath;
    }
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

And it seems to work fine for the first time, but when I select the row for the second time it throws me out and says EXC_BAD_ACCESS. and when I switch the simulator to 4.3, it selects the row only ones and then doesn't work. Can anybody help?

Alick Dikan
  • 65
  • 2
  • 3
  • 10
  • How does the lastIndexPath @property look like? – phi Nov 29 '11 at 15:48
  • @property (nonatomic, retain) NSIndexPath *lastIndexPath; like this – Alick Dikan Nov 29 '11 at 15:50
  • 1
    How is lastIndexPath declared? My guess is that is the issue. The first time through lastIndexPath is nil, so no problem there. The second time through, it's a pointer to an object that has been released (indexPath from the previous call to didSelectRowAtIndexPath). – Nathanial Woolls Nov 29 '11 at 15:51
  • If it's a property you need to use self.lastIndexPath - currently you are avoiding the getters/setters and thus avoiding proper retain/release mechanics. – Nathanial Woolls Nov 29 '11 at 15:51

1 Answers1

4

Use self.lastIndexPath = indexPath if you have a @property declared for lastIndexPAth.

phi
  • 10,634
  • 6
  • 53
  • 88