ghostrider
ghostrider

Reputation: 5259

segue in a table cell gets called every time

I have a tableview and I have plugged in a segue in its cell. i want given a condition, to either enable or disable the cell.

This is my code inside the didSelectRowAtIndexpath

if (condition) {
    NSLog(@"Nothing);
} else {
    [self performSegueWithIdentifier:@"segueName" sender:tableView];
}

But segue gets called every time, even if I do see the NSLog printed.

Some answers here said that I should place the segue in the view controller but in the storyboard I cannot do it, I can only place it into the cell.

What have I missed?

Upvotes: 2

Views: 298

Answers (1)

Rob
Rob

Reputation: 437422

Do you also have the segue going from the cell to the next scene in Interface Builder? If so, that will be called, regardless of what didSelectRowAtIndexPath does.

Two approaches are possible:

  1. If iOS 6-only, you can eliminate didSelectRowAtIndexPath altogether, put the segue between the cell and the next scene, and then implement shouldPerformSegueWithIdentifier and put your conditional logic there, e.g.

    - (BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender
    {
        if ([identifier isEqualToString:@"segueName"])
        {
            if (condition)
                return NO;
        }
        return YES;
    }
    
  2. If you need compatibility with iOS 5, you should remove the segue between the cell and the next scene, but have it go between the view controllers (i.e. you control-drag from the first view controller's icon in the bar at the bottom of the scene to the next scene). That way, the segue is defined, but not hooked up to the cell. And your didSelectRowAtIndexPath can use the conditional logic you suggest in your question:

    segue between scenes

Upvotes: 3

Related Questions