Can I refer to NSDictionary items numerically?
I'm trying to set up a UITableView which acts as a form. Each cell has within it a UILabel and a UITextField, so one cell is:
Name <enter name>
^ ^
UILabel UITextField
Now, I'm trying to popula开发者_运维问答te the UILabel and the UITextField from a NSDictionary (from a plist), where it's organized like so:
Root Type Value
Name String
Address String
City String
etc
But in order to get the right labels and textfields in the cellForRowAtIndexPath method, I would have to refer to the Dictionary numerically. Is there any way to do that?
No, since elements in an NSDictionary are not ordered.
You should really use an array as the model behind a table view data source. You can easily build one for your case:
NSArray* myArray = [myDict allKeys];
Of course, for your case, a special order would be neccessary. So you would rather build your array using literals:
NSArray* myArray = [NSArray arrayWithObjects:@"Name", @"Address", @"City", nil];
Strictly speaking, no.
What I tend to do is create a separate NSArray of keys, which are ordered so that things come out in the right sequence. eg:
NSDictionary *values = ...
NSArray *keys = ....
for (NSString *key in keys) {
id value = [values objectForKey:key];
...
}
As others have pointed out, it is necessary to have an ordered list (ie, an NSArray
) to match the way that a UITableView
works. However, this doesn't have to be the data itself. When the data is a NSDictionary, or an NSManagedObject, there is a common idiom of editing that data in a form, which on iPhoneOS will almost always mean a UITableView
.
In this case, you very probably won't want to show all the elements in the edited object, you will want to display titles for the fields, and will want to control the order of display. All of which means that this is a valid (not obfuscated) pattern for the situation.
In this particular case, the keys could be taken from the plist, but extracting them in a general way, although feasible by parsing the plist XML, is non-trivial.
You should restructure your plist to be an array of dictionaries. The dictionary keys are the element that you need to populate and the values the text. Add each dictionary to the plist array in the order that you want it to appear on screen. This is effectively how the Settings application works. Saving the text entered by the user is very straightforward.
Your plist will look something like:
<root>
<array>
<dict>
<key>labelText</key>
<value>Name</value>
<key>textFieldPromptText</key>
<value>enter name</value>
<key>textFieldText</key>
<value>name entered by user</value>
</dict>
<dict>
<key>labelText</key>
<value>Address</value>
<key>textFieldPromptText</key>
<value>enter street address</value>
<key>textFieldText</key>
<value>address entered by user</value>
</dict>
<dict>
<key>labelText</key>
<value>City</value>
<key>textFieldPromptText</key>
<value>enter city</value>
<key>textFieldText</key>
<value>city entered by user</value>
</dict>
.
.
.
</array>
</root>
精彩评论