SRMR SRMR - 1 year ago 113
iOS Question

didSelectRowAtIndexPath gets called but not prepareForSegue

This only happens when I've run the app fresh for the first time, and even then, when I try to recreate to debug, most of the time it works correctly.

I select the song at the

, and everything logs correctly, except the segue doesn't get called.

It calls
, but doesn't call

Everything is hooked up right, because if I try it again it works perfectly and both
get called. (I'm just using
for debugging to log that I'm not getting nil data.)

Has anyone run into this, or have any ideas of how to debug this? Thanks!

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
MPMediaItem *selectedItem = [[albumsArrayForTVC objectAtIndex:indexPath.section] representativeItem];
NSString *albumDetailTitle = [selectedItem valueForProperty:MPMediaItemPropertyAlbumTitle];
MPMediaQuery *tempAlbumMPMediaQuery = [MPMediaQuery songsQuery];
MPMediaPropertyPredicate *albumPredicate = [MPMediaPropertyPredicate predicateWithValue: albumDetailTitle forProperty: MPMediaItemPropertyAlbumTitle];
[tempAlbumMPMediaQuery addFilterPredicate:albumPredicate];
NSArray *albumTracksArray = [tempAlbumMPMediaQuery items];
MPMediaItem *rowItemSong = [[albumTracksArray objectAtIndex:indexPath.row] representativeItem];
NSLog(@"track: %@", rowItemSong);
NSString *song = [rowItemSong valueForProperty:MPMediaItemPropertyTitle];
NSLog(@"track name: %@", song);

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

if([segue.identifier isEqualToString:@"nowPlaying"]){

// send to now playing
NSUInteger selectedSection = [[self.tableView indexPathForSelectedRow] section];
NSUInteger selectedIndex = [[self.tableView indexPathForSelectedRow] row];
NSArray *albumTracksArray = [self albumTracksForSegue:[[albumsArrayForTVC objectAtIndex:selectedSection] representativeItem]];
MPMediaItem *rowItemSong = [[albumTracksArray objectAtIndex:selectedIndex] representativeItem];
MPMusicPlayerController *musicPlayer = [MPMusicPlayerController systemMusicPlayer];
[musicPlayer setQueueWithItemCollection:[MPMediaItemCollection collectionWithItems:albumTracksArray]];
[musicPlayer setNowPlayingItem:rowItemSong];
[musicPlayer play];
NSLog(@"Row Item Song: %@", rowItemSong);

enter image description here

It's a weird one because its not something that I can recreate every time, thats why its extra hard for me to figure out what's going on here, whether it's something with the system music player or whether it's something in my code, etc.

Answer Source

Call [self performSegueWithIdentifier:@"YourSegueIdentifier" sender:nil]; in your didSelectRowAtIndexPath: method. Note that both didSelectRowAtIndexPath: and prepareForSegue: are methods of UIViewController and not of UITableViewCell.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
    //If you have only one segue then do just this else add your if condition for different segues 
    [self performSegueWithIdentifier:@"YourSegueIdentifier" sender:nil]; 
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download