user2114872
user2114872

Reputation: 49

Filter NSDictionary using DidSelectRow selection as key/filter object

I parsed and stored an XML in two mutable arrays,they are albumArray and trackArray. I created an dictionary using these two arrays and that is as follows,

 trackANDAlbum = [NSMutableDictionary dictionaryWithObjects:trackArray forKeys:albumArray];

so my dictionary looks like this :

 album1 = song1 
 album1 = song2
 album1 = song3 etc. 

Since the albumArray contains duplicates, I eliminated it using NSSet and this new array called "eliminateDupe" is given as the data source for a UITableView.

The problem I face is that, when the user selects a particular album name in the TableView then, the corresponding songs of the selected album must be displayed in another view.

So is it possible to identify what album name is selected in DidSelectRow of TableView and provide that as a key for the dictionary trackANDAlbum, and retrieve the corresponding songs and display it in an tableview.

For eg, if the selected album is "album1" so in the next tableview songs corresponding to album1 must be listed,that is song1,song2,song3.Any possibilities to achieve this concept, or else do I have some better ways?

Upvotes: 0

Views: 104

Answers (1)

Dilip Manek
Dilip Manek

Reputation: 9143

If you are using albumArray to populate your tableview than using this code you can get album name on touch of row in tableview.

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
         NSString * albumName = [albumArray objectAtIndex:indexPath.row];
        }

And you can use this albumName variable as key to find song name from your dictionary, if you are using another array then change array name according,and yes if you want to use albumName variable outside of this method than make its property in .h file.

UPDATE

For retrieving data from dictionary using our albumName variable

if([yourDictionary objectForKey:albumName]!=nil)
   {               
     NSString * songName = [yourDictionary objectForKey:albumName];
   }

You can retrieve data using this code, and yes as I said to use albumName in another method you have to make its property in .h file and synthesize it in .m file.

EDIT

In your case you have multiple song name for single album name so your code going to be like this.

if([yourDictionary objectForKey: albumName]!=nil)
       {     
          NSArray * newSongAry;
         [newSongAry addObject:[yourDictionary objectForKey:albumName]];
       }

Upvotes: 1

Related Questions