I have a PLIST file that stores issue information. At the top is an array, and within that array there are dictionaries. Within each dictionary is a string called "Date" with different dates. I then display the date values in a table view.
I am able to determine the text of the UITableview cell tapped (and therefore the date string), but how do I then access the other values within the same dictionary? If the I know that the date string is February 16, 2012, how do I get the "Download URL"? Here is a picture of my PLIST:
You should be getting the indexPath from the cell that is tapped. The indexPath.row should give you an index number that you can use to get the correct dictionary from your array of dictionaries. If you are not already keeping this array, consider doing so if it's a manageable size.
NSDictionary *selectedIssue = [myArrayOfDictionaries objectAtIndex:indexPath.row];
NSSTring *downloadURL = [selectedIssues valueForKey:#"Download URL"];
Personally, I would never rely on the date being unique or formatted in any particular way.
You can sort your array (Issue List) by the issues date before you init your TableViewController with that data and than use the NSIndexPath of the cell that was tapped.
To sort the array with the dictionaries use this snippet:
NSSortDescriptor *descriptor = [[NSSortDescriptor alloc] initWithKey:#"date" ascending:YES];
[issues sortUsingDescriptors:[NSArray arrayWithObjects:descriptor,nil]];
After that you can use the indexPath like that:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *url = [[issues objectAtIndex:indexPath.row] valueForKey:#"download_url"];
}
Related
I was following a tutorial: http://www.devx.com/wireless/Article/43374
so I could add alphabet sorting and panning to my UITableView of songs and I have finished coding but this method I have followed slows down the UITableView by filtering arrays and retrieving values in the cellForRowAtIndexPath method.
I cant figure out how I can remove the excess coding to increase the speed. All the MPMediaItems are stored in the tableTracks array. Which is initialized in viewDidLoad. And thee musicIndex is an array of the alphabets(first letter of each song). I extended MPMediaItem to include an NSString firstLetter that is the first letter of the song.
Any help speeding it up?
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//--Create Cell--\\
.........
//--Load Info--\\
NSString *alphabet = [musicIndex objectAtIndex:[indexPath section]];
NSPredicate *predicate =
[NSPredicate predicateWithFormat:#"firstLetter beginswith[c] %#", alphabet];
NSArray *songs = [tableTracks filteredArrayUsingPredicate:predicate];
//Needed Object
MPMediaItem *item = [songs objectAtIndex:indexPath.row];
//--Rest of Method--\\
...........
return cell;
}
If you are showing separate sections, one for each letter of the alphabet, I would create an array of dictionaries from my data, in viewDidLoad, not here. The dictionaries would have the first letter of the song as the key, and an array of songs as the value. That way, all the filtering and sorting is done up front, rather than in each row as the table is populated.
Is there a way to reverse the tableView order?
For example, I have a tableView that sorts by firstUpdated to LastUpdated. It does automatically cause its composed by plist data. But what if I want to put the newest data on top and the older on bottom?
The other solution will work fine but this one is a bit shorter.
NSArray *reversedArray = [[originalArray reverseObjectEnumerator] allObjects];
You should sort the array you use to populate the table view.
The other two answers would work, but I have another way of doing this if you need to sort by any property of the object:
You can create a method -sortMyArray
and it will look like:
-(void)sortMyArray
{
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:#"SomeObjectSortProperty" ascending:YES];
NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
[MyArray sortUsingDescriptors:sortDescriptors];
[sortDescriptor release];
}
and of course, every time after you call this method, you need to call your table view reload data method.
Hope this helps.
Another alternative (to maintain only a copy of the original array), is to use the original array and just grab objects in backwards order in the UITableViewDataSource functions.
Example:
id currentObject = [originalArray objectAtIndex:(originalArray.count - indexPath.row - 1)];
This is actually probably a better solution because you don't need to maintain 2 copies of the same data simply for reversed order.
You were using the array to populate the data in table view ...
Sort that array rather than sorting the table order..
self.yourCurrentArrayOfObjects = ...;
NSMutableArray *reversedArray = [NSMutableArray arrayWithCapacity:yourCurrentArrayOfObjects.count];
for (id object in yourCurrentArrayOfObjects.reverseObjectEnumerator)
{
[reversedArray addObject:object];
}
self.yourCurrentArrayOfObjects = [reversedArray copy];
[self.tableView reloadData];
I am making an iPhone app with different views, one of these being an UITableView, and i want to pass an ID string to another view, depending on the selected row.I have multiple row selection.I don't want to pass the data of the cell, but an ID to associate with it.
F.E: Cell name: "United States", to return a NSString: "09r454-0567-34".I don't have an idea of how to associate these strings to a cell.Thanks in advance.
Maybe you need to declare an array of dictionaries, then you catch didSelectRowAtIndexPath:, and retreive the information in that array
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSDictionary *dict = [myIDArray objectAtIndex:indexPath.row];
NSLog(#"ID %#", [dict objectForKey:#"ID"]);
NSLog(#"Name %#", [dict objectForKey:#"Name"]);
}
NSArray* myIDArray; //must be declared in .h
Hope this help...
I used iphones address book data in my app ,i sorted that data and arranged in UITableViewController,but now i want to make same look as of address book means sorted in GroupedStyle having initial character as heading of section .and suggestion plz
You have to implement the tableView methods that are
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
for section headers and for this this you also have to implement
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
By doing this your table data can be viewed in grouped style.
Use something like following to sort the contacts:
NSArray* tempArray = [jsonData objectForKey:#"contacts"];
NSSortDescriptor *sortDescriptor;
sortDescriptor = [[[NSSortDescriptor alloc] initWithKey:#"first_name"
ascending:YES] autorelease];
NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
contactsArray = [[NSArray alloc] initWithArray:[tempArray sortedArrayUsingDescriptors:sortDescriptors]];
From the use NSPredicate to pick the contacts and add them to section that you want.
I have a tableView with several Sections being populated from a plist of NSDictionaries
How do I have it arrange the sections in the order they are in in the NSDictionary instead of alphabetically?
NSDictionary is unordered. You should use an NSArray (or std::vector, or std::map, etc.) as the data source.
To get the keys, use -allKeys. To get a sorted array from it, use -sortedArrayUsingSelector:.
I improvised a solution. I added a "01", "02", "03" etc to the beginning of each dictionary name then just added the code to remove those two characters before displaying it:
-(NSString *)tableView:(UITableView *)tableView
titleForHeaderInSection:(NSInteger)section{
NSString *key = [keys objectAtIndex:section];
key = [key substringFromIndex:2];
return key;
}