-1

I try use storyboard with table view that segue to controller view that should show me picture. The problem is that the app crush when I press on any "cell".

this is the problem code:

- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{

    if([[segue identifier] isEqualToString:@"ShowPhoto"])
    {
        DetailsViewController *dvc = [segue 
        destinationViewController];
        NSIndexPath *path = [[self tableView] indexPathsForSelectedRows];
        Photo *pic = [photosArray objectAtIndex:[path row]];

        [dvc setCureentPic: pic];
    }
}

the problem is the line with NSIndexPath... I think that the reason is that I try insert array into NSIndex. xcode warning: Incompatible pointer types initializing 'NSIndexPath *__strong' with an expression of type 'NSArray.

the crush message: unrecognized selector sent to instance [__NSArrayI row]. how can I solve the problem?

Dennis
  • 704
  • 1
  • 9
  • 25

1 Answers1

6

Please do read the documentation (here) of the methods you call instead of making assumptions about what they do...

[[self tableView] indexPathsForSelectedRows]

returns an NSArray and not an NSIndexPath (by the way, you could know this even without reading the docs - in Cocoa naming convention, if something is in plural, then it accepts or returns an NSArray), so sending it the messages of NSIndexPath will crash. Write

NSIndexPath *path = [[[self tableView] indexPathsForSelectedRows] objectAtIndex:0];

instead, and it will work (if there is at least one selected row - if not, you have to check for the array being empty, else it will crash again...)

By the way, the compiler even warns you about what the problem is... Please try to develop some common sense before trying to do programming, because if not, you will shoot yourself and others in the foot.

  • thanks but now when I checked docs I understood that I write wrong method, the right method should be: NSIndexPath *path = [[[self tableView] indexPathForSelectedRow]; – Dennis Nov 01 '12 at 11:30
  • 1
    @Dennis But then why did you unaccept my answer and accepted another? That answer is basically the same, but without any explanation... –  Nov 01 '12 at 11:44
  • I accepted your answer, I don't know why this uncheck your answer maybe because I accept the other guys answer too? (I'm new in stackoverflow). – Dennis Nov 01 '12 at 21:19
  • @Dennis Yes, one question can have only one accepted answer at a time, accepting another answer automatically unchecks the one (and this can be changed at any time, unlike votes). –  Nov 01 '12 at 21:20