0

I want Checkmark in particular TableView Cell.

So I have used code :

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

newRow = [indexPath row];
oldRow = [lastIndexPath row];

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

    UITableViewCell *oldCell = [tableView cellForRowAtIndexPath: lastIndexPath]; 
    oldCell.accessoryType = UITableViewCellAccessoryNone;

    lastIndexPath = indexPath; 
}
else{
    UITableViewCell *newCell = [tableView cellForRowAtIndexPath:indexPath];
    newCell.accessoryType = UITableViewCellAccessoryCheckmark;
    lastIndexPath = indexPath;
}}

Its working in iPhone Simulator fine. But while testing in iPhone device, it crashes the application.

Any solution for this..?

Thanks in advance.

Nitin
  • 7,455
  • 2
  • 32
  • 51
Manann Sseth
  • 2,745
  • 2
  • 30
  • 50

2 Answers2

2

The most likely cause of your crash is in your ivar, lastIndexPath. You're storing values in this without retaining them, so they may be released at any time. Try defining a property named lastIndexPath (retain for manual reference counting, strong for automatic reference counting). Then you can use self.lastIndexPath = indexPath or [self setLastIndexPath:indexPath].

Also, it's bad to forcibly change a cell contents like this. It's better to store the selected index, then reload the table data. For maximum efficiency, only update the changed cells. Have your cellForRowAtIndexPath: method switch the checkmarks on and off.

Cowirrie
  • 7,218
  • 1
  • 29
  • 42
  • Thanks. I have used as you suggested. I have created property for lastIndexPath(NSIndexPath) and synthesize it.. And its working in simulator fine. But still problem in iPhone Device. – Manann Sseth Apr 25 '12 at 05:35
  • 1
    Are you using your new property fully? Have you replaced `oldRow = [lastIndexPath row];` with `oldRow = [[self lastIndexPath] row];` and both cases of `lastIndexPath = indexPath;` with `[self setLastIndexPath:indexPath];`? – Cowirrie Apr 25 '12 at 05:55
  • 1
    Can you edit your post to show the revised code, both of `cellForRowAtIndexPath:` and `didSelectRowAtIndexPath:`? If it manages to identify the line that caused the crash, that would also be good to know. – Cowirrie Apr 25 '12 at 12:25
  • Hey now got it solution without error. I'll tell you after check in device.. Anyways thanks. – Manann Sseth Apr 25 '12 at 12:34
0

Simply write following code in "cellForRowAtIndexPath" method.

[[cell imageView] setImage:[UIImage imageNamed:@"tickmarkBlue.png"]];
[[cell imageView] setHidden:YES]; 

if(<your condition here>)
{
      [[cell imageView] setHidden:NO];
}
βhargavḯ
  • 9,786
  • 1
  • 37
  • 59
  • Applying the changes in `cellForRowAtIndexPath:` is correct. However, this doesn't directly address the original poster's crash problem. Also, was this code written for some task that `[cell setAccessoryType:UITableViewCellAccessoryCheckmark]` wasn't suited to? – Cowirrie Apr 25 '12 at 04:44