Alternate way to get object based on indexPath in UITableView - iphone

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.

Related

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

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

how to search nsmutable array in objective C

can any body tell me how to search NSMutable Arry in which objects are stored from xml feed
i have the following code
- (void) searchTableView {
NSString *searchText = searchBar.text;
NSMutableArray *searchArray = [[NSMutableArray alloc] init];
//blog entries is the nsmutable array in which objects are stored from RSS feed
for (NSMutableDictionary *dictionary in blogEntries)
{
NSArray *images=[dictionary objectForKey:#"image"];
NSArray *titlearray = [dictionary objectForKey:#"title"];
NSDictionary *imagesDic=[NSDictionary dictionaryWithObject:images forKey:#"image"];
NSDictionary *titlearrayDic=[NSDictionary dictionaryWithObject:titlearray forKey:#"title"];
[searchArray addObject:imagesDic];
[searchArray addObject:titlearrayDic];
}
//know the problem comes in below code i just want to compare title not image string as there any way to search only of title array not for both image in title some what like this For(nsstring *stemp in searcArray.titleArray etc)
for (NSString *sTemp in searchArray)
{
NSRange titleResultsRange = [sTemp rangeOfString:searchText options:NSCaseInsensitiveSearch];
if (titleResultsRange.length > 0){
[copyListOfItems addObject:sTemp];
}}
the problem is that this code just saving title not image and if i save image then it also search in image string which i dont want to do. i want the user will search only by title then when he type something in textbox if search is true against some values then only thos e are displayed in table cell with title and image.
as this is RSS APPLiction and feeds are comming from xml feed
which
click here
bescially i am extracting this xml feed and em displaying image and title tage in table cell know i want to implement searchbar in it
Thanks....i am waiting for your response...
#mipadi is right - try using containsObject: first.
If that doesn't work, just a simple loop will do it - you can put in whatever matching criteria you want in there. e.g. This code searches by comparing the name properties :
- (id)searchArray:(NSArray *)haystack for:(id)needle {
for(id temp in haystack)
if ([temp name] isEqual:[needle name]])
return needle;
return nil;
}
Hope that helps.
NB If you're using your own objects in the array, you can use containsObject: if you have overridden isEqual: (and hash)
Depends on how you want to search. If you're just looking for a particular object, you can use containsObject:.
Without knowing more about what you want, it's tough to answer your question. But here are some starting points:
NSArray - Check out the methods starting like indexOfObject-; I think one of these probably does what you want. There's also filteredArrayUsingPredicate.
NSMutableArray - The only notable method here is filterUsingPredicate, I think.
Hope one of these helps you.

UITAbleView not in alphabetical order?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;
}