Search code examples
iphoneuitableviewuiviewdidselectrowatindexpathnsindexpath

'indexPath' undeclared (first use in this function)


I have the following error. 'indexPath' undeclared (first use in this function).

Code. didSelectRowAtIndexPath

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

UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle: UIActivityIndicatorViewStyleWhiteLarge];
cell.accessoryView = spinner;
[spinner startAnimating];
[spinner release];

[self performSelector:@selector(pushDetailView:) withObject:tableView afterDelay:0.1];
}

pushDetailView

- (void)pushDetailView:(UITableView *)tableView {

// Push the detail view here
[tableView deselectRowAtIndexPath:indexPath animated:YES];
//load the clicked cell.
DetailsImageCell *cell = (DetailsImageCell *)[tableView cellForRowAtIndexPath:indexPath];

//init the controller.
AlertsDetailsView *controller = nil;
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad){
controller = [[AlertsDetailsView alloc] initWithNibName:@"DetailsView_iPad" bundle:nil];
} else {
controller = [[AlertsDetailsView alloc] initWithNibName:@"DetailsView" bundle:nil];
}

//set the ID and call JSON in the controller.
[controller setID:[cell getID]];

//show the view.
[self.navigationController pushViewController:controller animated:YES];
}

I think it's because I'm not parsing the indexPath value from didSelectRowAtIndexPath to pushDetailView but I don't know how to approach this.

Could someone please advise?

Thanks.


Solution

  • The problem is that you pushDetailView: method has no indexPath variable on it's scope.

    Instead of

    - (void)pushDetailView:(UITableView *)tableView {
    

    You should make your method like this:

    - (void)pushDetailView:(UITableView *)tableView andIndexPath: (NSIndexPath*) indexPath {
    

    and then indexPath would be declared on the method scope.

    Then, inside your didSelectRowAtIndexPath method, replace

    [self performSelector:@selector(pushDetailView:) withObject:tableView afterDelay:0.1];
    

    for the code bellow:

    double delayInSeconds = 0.1;
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
        [self pushDetailView: tableView andIndexPath: indexPath];
    });
    

    This uses GCD to execute the code after a delay, instead of the performSelector: withObject :afterDelay: and here is a nice post explaining why sometimes is better to opt for this aproach