Iphone : How to get array value in didSelectRowAtIndexPath? - iphone

I am using google Api for custom search and have done the json Parsing. I succesfully retreived the values from JSON and store that data in Dictionary.
NSArray *latestdata = [(NSDictionary*)[responseString JSONValue] objectForKey:#"items"];
Now I have two array in it link and title
titleArray = [latestdata valueForKey:#"title"];
linkArray = [latestdata valueForKey:#"link"];
Now i use the title array to store the title at CellForRowAtIndexpath and i want to use the link for that specified or selected index at DidSelectIndexPathAtRow.
I have already filled the cell by the title array as below
cell.textLabel.text = [title objectAtIndex:indexPath.row];
But now i dont when i select the row at table how can i get the link for that using the link array at didSelectRowAtIndexPath method.
What could be the solution for this.

Try following code at didSelectRowAtIndexPath
NSLog("Value : %#", [title objectAtIndex:indexPath.row]);
I hope title is global array.

You would use the same paradigm you set the title with. You have indexPath.row available to you in didSelectRowAtIndexPath.
My worry with this is ensuring the arrays are in sync. Would you no be better served using a Map for the data? If you are more comfortable, you can store the titles in an array, because parsing that array to build your table is really straight forward. But how do you know the element in each spot in the title array matches its corresponding member of the link array? Are you sure [title objectAtIndex: 1] matches the desired link in [link objectAtIndex: 1]? Key-value coding will harden this and give you an easier to maintain solution in my opinion.
Good Luck.

Related

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

Decrease indexPath by one

I'm filling a TableView with CoreData.
Until now I was doing something like this:
NSManagedObject *managedObject = [self.fetchedResultsController objectAtIndexPath:indexPath];
...to retrieve the object to fill the row.
Everything was working fine until I realized I have to manage the first row of the table as an exception because the first line will contain other content, not provided by CoreData.
Now my issue is how can I manipulate the indexPath to shift everything by one. I would like to do something like this:
// I know this is not going to work, just to give you an idea...
NSManagedObject *managedObject = [self.fetchedResultsController objectAtIndexPath:indexPath-1];
but I cannot find the right syntax to manipulate the indexPath. Can anyone help me?
Thx for your time!
In case we are talking about iOS UITableView index path there's much easier way:
NSIndexPath* newIndexPath = [NSIndexPath indexPathForRow:oldIndexPath.row+1 inSection:oldIndexPath.section];
Cheers... :)
If you're talking about a standard iOS UITableView, then your index path is an instance of NSIndexPath, and will have two entries: a section index and a row index. If I understand you right, you want to decrement the row index by 1 every time you go to fill a table view cell. (I'm assuming you only have one section, or don't care about the section index - if this is wrong, please edit your question.)
Basically what you need to do is construct a new NSIndexPath instance with your adjusted indices, then use that to query your fetched results controller. Something like this would work (untested):
NSUInteger indexes[2];
[indexPath getIndexes:indexes];
indexes[1]--;
NSIndexPath * adjustedIndexPath = [[NSIndexPath alloc] initWithIndexes:indexes
length:2];
NSManagedObject * managedObject = [self.fetchedResultsController
objectAtIndexPath:adjustedIndexPath];
This basically does the following:
Pulls the existing indexes into a C array of NSUIntegers
Decrements the last index (at position 1 - the row index) by 1
Creates a new NSIndexPath with the adjusted indexes from the C array
Fetches the managed object using the new index path
Note again that this doesn't touch the section index at all, and so will adjust every cell in your table, regardless of whether it's in your first section. If that's not what you want, either wrap the adjustment in a conditional (e.g. if([indexPath indexAtPosition:0] == 0)) or add your own custom logic.

NSTableView rows using NSArray

I need to Write 4 sections in a table where two of them have only a single row.
So is there any other way i could do it instead of using NSArray for just a single row?
You can create two dimensional array using NSMutablearray
You can access them by using
[[array objectAtIndex: ] objectAtIndex: ]
Hope it helps.....
Dont know from where you are getting the data for your 2 single rows but you could make a switch(indexPath.section)/case or an if(indexpath.section == yoursectionnumber) logic in your cellForRowAtIndexPart delegate method and there you can assign a value to your cell.
As DShah wrote, you can use variables and such for this so you could for example do a cell.textLabel.text = myString;
Where myString is an NSString created by you.

How to parse XML array in iPhone?

I am doing parsing, in my parsearray having only two data number of alerts and number of events these are fine. When we are finding the value like
NSString *alertcount = [[xmlparseArray objectAtIndex:indexPath.row ]objectForKey:#"alerts"];
and assigning these string value into label
mylabel.text = [NSString stringWithFormat:#"%#", alertcount];
same for Events.
then it becomes crash. It says:
index 0 beyond out of empty Array
And these parsing I am using in #RootViewController:
UITableViewController {
}
not using custom cell. In this rootviewcontroller class we are using 7 row(these are constant) and I want to assign at row number 5 the value of alert (i.e. mylabel.text = [NSString stringWithFormat:#"%#", alertcount]) at the right position of cell number 5 same is cell number 6.
Your xmlparseArray might be empty. Check if you have any elements in the array. Pls show the code where you populate the array so that we can have a better idea of where you are going wrong

iPhone - Sort UITableView by Array Index

I have an UITableView set up with a NSArray with 10 indexes. Right now, the first cell on the uitableview is the first index, the second cell is the second index, and so on so forth. Is there any way to make it so that the first cell displays the latest index? Maybe through some code in the uitableview delegate because I am adding data to the NSArray. What that means is that there aren't 10 indexes right off the bat.
If anyone as an answer, help is much appreciated.
Each time that you get a new item of data, you add it to the start of your array, not to the end. Then just call [self.tableView reloadData] and it should just work.
You can use insertObject:atIndex: to add to the start of the array:
[myArray insertObject:newData atIndex:0];
(see here for docs)
Somewhere in your code you're probably doing something like this (where items is your NSArray object):
cell.textLabel.text = [items objectAtIndex:indexPath.row];
Instead, do:
cell.textLabel.text = [items objectAtIndex:([items count] - 1) - indexPath.row];