UITAbleView not in alphabetical order?iPhone - iphone

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

Related

Alternate way to get object based on indexPath in UITableView

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.

I want to populate my tableview from my NSDictionary

- (void)receiveData:(NSData *)data {
self.office = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
self.files = [office objectForKey:#"files"];
[self.myTableView reloadData];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.files.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [[UITableViewCell alloc] init];
NSDictionary *file = [self.files objectAtIndex:indexPath.row];
cell.textLabel.text = [file objectAtIndex:1]; //here i want to get data from the array
cell.textLabel.font = [UIFont boldSystemFontOfSize:15];
cell.textLabel.numberOfLines = 1;
return cell;
NSLog(#"files:\n%#", file);
}
I have a json response that i have stored in NSDictionary offices, and array in that json is stored in NSArray files,now i have given files array to another NSDictionary file and according to data that stored in that file i want that data in my table row, but i don't have any "key" in my files array only value in that array,
so my question is that how can i point that value in that my file dictionary, i have make comment where i want that please see that comment....
You're creating a dictionary:
NSDictionary *file = [self.files objectAtIndex:indexPath.row];
NSDictionary is indexed with keys, so you'll have to obtain the string with objectForKey:
cell.textLabel.text = [file objectForKey:#"fileName"];
I think you must either misunderstand how an NSDictionary works or else the objects that you're accessing in self.files are not actually NSDictionaries. If they are in fact NSDictionary objects they need to be accessed like runmad says with
[file objectForKey:#"keyName"]
If there are no keys then you are dealing with a different type of object and we would need to know more specifics to be able to help you. As you posted the question runmad has given you the correct answer.
The JSON parser NSJSONSerialization may not be creating the NSDictionary/NSArray structures you think you have. You'll have to check your JSON, e.g. with jsonlint.com, then use NSLog to have a look at what is placed in your dictionaries/arrays. Also try some of the NSJSONReadingOptions options.

Determine which dictionary an object came from

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

Creating Array for Sections in UITableView

I'm creating a tableview from a plist which contains an array of 6 dictionaries.
One of the fields in the dictionaries is LastUpdatedDate, which I'd like to use as the table section value.
I'm trying to populate the section array using the code below, but nothing is added.
Any ideas?
BOOL found;
for (NSDictionary *document in documents){
NSString *date = [document objectForKey:#"LastUpdatedDate"];
found = NO;
for (NSString *str in sections){
if ([sections containsObject:str])
{
found = YES;
}
}
if (!found)
{
[sections addObject:date];
}
Hmmm ... you are looping through all strings in sections, and for each string in sections you check whether it is part of sections - of course it is always TRUE and found therefore YES.
You probably want to check whether date is contained in sections ;-).
The for-loop makes no sense. You check if every String from the sections-Array is in the sections-Array, that is always true! It is enough to check if the date is in the sections-Array
[sections containsObject:date]
Did you get a valid date-String that should be added to the sections-Array? How do you know if the sections are empty? What is the count of the sections-array?
[sections count]
Well the addObject-call looks good...

Getting an ID string when selecting a cell

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...