I have created an application that populates a table from JSON data I pull from my local host. I am able to search through this data using a search bar and have added a button to add the rows to another table which I am using a library for a users favourite objects. So now that Ive this all working I'm trying to create a detail view that when a user clicks on a row in the table they are brought to a new view controller and then they can view the full details of the object as the rows in the initial table only have the title and author listed in each row.
I have made the Segue and created the view controller ect and when I try to populate a few UILabels on the detaiViewController I get a error:
Maybe papers dictionary store others NSDictionary, not Paper object. When you parse a JSON, it will parse to iOS SDK class, such as [] parse to NSArray, "" parse to NSString, {} parse to NSDictionary.
So, I guess your JSON will parse to an array of dictionary. It cannot return a Paper for you. You have to assign values in each dictionary to one your Paper, and put it in a NSArray of Paper.
Change:
self.Title.text = self.paper.Title;
to:
self.Title.text = self.paper[#"Title"];
Use awakeFromNib instead of viewDidLoad
NSMutableArray *locations = [[NSMutableArray alloc] init];
_papers = [json objectForKey:#"Papers"];
You are populating the locations array from retrieving elements from _papers. Actually _papers holds list of NSDictionaries and when in prepareForsegue you just get an element from _papers which is a dictionary so thats why it is giving error. You have to store this locations array and retrieve elements from it because you are parsing the json into paper in the loop and putting it in locations hope it helps.
Related
I am looking for a data structure that allows me to add multiple objects for the same key.
I have a table that is downloading images and when it finishes downloading an image I want it to set the image in all the cells with that image name.
So I want a data structure where the key is the image name and it multiple objects i.e pointers to the cells.
I know I can't do this in NSMutable dictionary as it just overwrites the keys. Is there another way?
Simply create a NSDictionary containing a NSMutableArray :
NSDictionary *myDico = #{#"imageName":[NSArray arrayWithObjects:...]};
Where ... = the references to the cells.
Edit :
You also can, if you have multiple images, use a NSMutableDictionary.
How about using NSMutableDictionary whose value is a NSMutableArray? You can append the array when new cells are added or check nil and add an empty array when the key is not stored before.
Just use NSArray to add multiple values against single key.
NSDictionary *dictt = #{#"imageKey":#[#"image1", #"image2"]};
So I have a little problem here, I think I made my point clear in the title of this post. I want to sort UITableView headers according to the order I declared them in code. I tried surfing the net already but I can't find any solution.
I think one from here can help. Thanks in advance!
EDIT: This is my code to of the table view contents
NSDictionary *temp = [[NSDictionary alloc]initWithObjectsAndKeys:accSettingsOpts, #"Account",notifSettingsOpts,#"Notifications",aboutOpts,#"About",logoutOpt,#"Sign Out",nil];
I want to display the table view sorted by
-Account
-Notifications
-About
-Sign Out
But it displays as
-About
-Account
-Notifications
-Sign Out
EDIT: This is how the problem is addressed.
Based from the answer provided in which I accepted below, I modified it as
#interface myClass ()
NSArray *headerArr;
#end
at viewDidload I added
headerArr = [[NSArray alloc]initWithObjects:#"Account",#"Notifications",#"About",#"Sign Out", nil];
and lastly...
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
return [headerArr objectAtIndex:section];
}
Thanks for the help guys. I didn't imagine that it's just that simple.
A dictionary is an unordered collection. If you store your items in a dictionary, the order in which you defined them is not saved anywhere.
Table views are an inherently ordered list of objects. Therefore a dictionary is not suitable as a way of storing the items for a table view. An array, however, makes a natural data source for a table view.
Use an array rather than a dictionary. It's as simple as that. You can use an array of dictionaries, or an array of some other data objects, and then write your cellForRowAtIndexPath method to extract the data from your model (array elements) and install the values in the cell.
The reason the order is different from the declaration order is that NSDictionary is unordered: it is sorted based on the hash code of your strings, which is rather arbitrary.
To force a particular order, use a container with a fixed order, such as NSArray. For example, you can store your accSettingsOpts, notifSettingsOpts, and so on, in a single array, add a header property to the Opts class, and pull that property to set section headers in your UITableView.
I am using an NSMutable Array as a datasource to UITableView. I am fetching the values from database and adding it to mutable array. When the view loads I populate the table view with the mutable array and it loads fine. Later when I click a cell it throws an error saying unrecognised selector sent to instance
Inside array I am storing a person object with a name property. I am loading all cells with person.name Later when I try to disolay person.name as the user clicks each cell it shows error.
This is how I do
Person *per = (Person*)[my arr objectAtIndex:indexPath.row];
NSlog(#"%#",per.name);
Here it crashes. But I am using the same code inside cellforRowAtIndexPath for displaying the person name.
Sometimes the error says Unrecognised selector name sent to NSArray
again sometime it says Unrecognised selector name sent to
UIDeviceRGBColor
So it means I am not getting correct object back from my NSMutable Array
How can I do this properly?
Thanks
This is due to the array is dealloacated. Make sure that the array is retained properly. In ARC have a strong reference. In non-ARC make sure you retain the array (I recommend to use property).
I have a data set displayed in a UICollectionviewController, which I access using [fetchedResultsController objectAtIndexPath:indexPath]
Using a segue, I move onto a modal scrollview+uipageController, where each data is displayed one after the other. The problem is to access each of them individually, as the view controller does not hold the fetch request. I'm thinking passing an array of object IDs, so that each page can fetch the associated data, from the data store.
my question is: given the fetchresultController, in the UICollectionviewController I'm seguing from, is there a way to quickly create the array of Object IDs, or do I have to loop through each managedObject, obtain their ManagedObjectID, and fill it into the array ?
You can use valueForKey to get all object IDs from an array of managed objects:
NSArray *objects = [fetchedResultsController fetchedObjects];
NSArray *objectIDs = [objects valueForKey:#"objectID"];
(Generally, sending valueForKey: to an array of objects applies valueForKey: to each object of the array, and returns an array of the results.)
Remark: When working with object IDs, keep in mind that these can change, see e.g. the objectID documentation:
Important If the receiver has not yet been saved, the object ID is a
temporary value that will change when the object is saved.
In an iPhone app, the User can create Items - each Item needs to have a CGPoint, NSString and a few integers with information about it. The User can keep adding these Items.
How can I store all these variables for each of the Items and programmatically keep adding them to a list or array or something?
I tried using a struct array but it can't hold a NSString. I tried using a NSMutableAray of a custom class, but I can only add them if I make and name them by hand.
Any suggestions, ideas? Could I use a NSDictionary?
Use an NSArray or NSMutableArray, but you have to wrap your non-object values (CGPoints and integers) in wrapper objects. The integers can be wrapped in NSNumbers and the points can be wrapped in NSValues.
Create an Item class, that contains your CGPoint, NSString, and any other piece of info you want. Then, make an NSMutableArray and add new instances of your Item class as you see fit. NSDictionary and NSArray aren't really what you're looking for.