Filter NSDictionary using DidSelectRow selection as key/filter object


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,


XML


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.


NSSet


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.


DidSelectRow



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?





You are displaying album name in tableview right?
– Dilip
Feb 27 '13 at 11:00





yes in a tableview
– user2114872
Feb 27 '13 at 11:16




1 Answer
1



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]];
}





Thanks for the reply.And I need to know how to filter the dictionary using this albumName ?.A sample code to help maybe.
– user2114872
Feb 27 '13 at 11:34





check now i have updated my answer.
– Dilip
Feb 27 '13 at 11:39





Thanks a lot ,it is working ,my problem solved .
– user2114872
Feb 27 '13 at 12:01





your welcome.....
– Dilip
Feb 27 '13 at 12:03






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

api-platform.com Unable to generate an IRI for the item of type

How to set up datasource with Spring for HikariCP?

Display dokan vendor name on Woocommerce single product pages