user711546
user711546

Reputation: 1

iPhone app freezes

Using iPhone Simulator 2.2.1

Using tables and wanted to add a check mark to a selected cell.

I added this snippet of code from an application that does work to a different application but uses the same method which also works. And now this new code compiles and launches and does put a check mark in the selected cell and a few seconds later the program freezes.

* This code section is from a working app.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

***This snippet was added (from another working app)

    if ([[tableView cellForRowAtIndexPath:indexPath] accessoryType] == UITableViewCellAccessoryCheckmark) 
        [[tableView cellForRowAtIndexPath:indexPath] setAccessoryType:UITableViewCellAccessoryNone];    
        else 
        [[tableView cellForRowAtIndexPath:indexPath] setAccessoryType:UITableViewCellAccessoryCheckmark];
    [self performSelector:@selector(deselect) withObject:nil afterDelay:0.5f];

***End of snippet

}

I put in some printf statements for the console and the execution does so through the if statement and past the last line.

So it works without the snippet and if I include the snippet it compiles, launches, and after selecting one item and the check mark appears, the app freezes.

The error message from the Console is below.

*** -[RootViewController deselect]: unrecognized selector sent to instance 0x526750
2011-04-16 16:15:30.132 lab3[37268:20b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[RootViewController deselect]: unrecognized selector sent to instance 0x526750'

Upvotes: 0

Views: 307

Answers (2)

Mark Granoff
Mark Granoff

Reputation: 16938

The answer is in the error message: There is no selector deselect for the object RootViewController which, in your code (and the snippet) is represented by self. The method deselect must be a method defined in the other program, but not in your current app. Cut-and-paste that in from the other program and I bet that solves the issue.

Another hint (unrelated to the problem, but with the code here): You can avoid all those calls to cellForRowAtIndexPath: if you declare a local variable before the if statement and then use that variable in the if statement, e.g.

UITAbleViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if ([cell accessoryType] == UITableViewCellAccessoryCheckmark)
    ....

You're performance will improve, once you solve the other issues.

Upvotes: 1

Caleb
Caleb

Reputation: 125007

You're sending a -deselect message to self, which is apparently an instance of RootViewController. However, your RootViewController class doesn't have a -deselect method, so it throws an exception, resulting in the error. So, either stop sending -deselect to self, or add a -deselect method to RootViewController.

BTW, a search for NSInvalidArgumentException would have turned up many, many similar questions, so you wouldn't have had to wait at all for an answer.

Upvotes: 0

Related Questions