Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

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];
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top