2

My Situation:

I have an NSDictionary object. Keyed by NSNumber. Values are custom objects.

I want to put dictionary values into a UITableView. As far as I can tell, UITableView requires that its source collection be indexed so when cellForRowAtIndexPath is called, you can use the indexPath to look up the value.

Problem is that when didSelectRowAtIndexPath is called, I want to look up the object from the dictionary, but I don't have the key. All I have is the indexPath.row.

My solution: I create an array of keys. I use the index of the array to get the key, and then use the key to get the object out of the dictionary.

My problem: This seems kind of sloppy especially since this is a routine task (populating the UITableView and then responding when someone touches a cell). Is this the way it's designed to work or is there a better way?

Trevor
  • 4,620
  • 2
  • 28
  • 37

2 Answers2

5

The problem is that dictionaries don't have an order, while a table view does. The answers to this question should give you some ideas for alternative ways of handling this.

Community
  • 1
  • 1
Jim
  • 72,985
  • 14
  • 101
  • 108
  • Frustrated and embarrassed that I didn't find that. But just what I was looking for. Thanks. – Trevor Jul 02 '11 at 22:49
  • 1
    I ended up using this:http://cocoawithlove.com/2008/12/ordereddictionary-subclassing-cocoa.html – Trevor Jul 03 '11 at 00:20
3

As mentioned in another answer, an NSDictionary's keys are not ordered, therefore you are not guaranteed to get the rows in a particular order. That said, it is quite easy to use a dictionary for use with a UITableView.

- (UITableViewCell *)tableView:(UITableView *)tableView 
         cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //  tableview cell setup

    NSArray* keys = [self.data allKeys];

    cell.textLabel.text = [self.data objectForKey:[keys objectAtIndex:indexPath.row]];

    return cell;
}

If you need the list to be ordered according to how you entered them into the NSDictionary, Matt Gallagher from Cocoa With Love offers an elegant solution with his take on OrderedDictionary. You can read about it here.

Wayne Hartman
  • 18,369
  • 7
  • 84
  • 116
  • You really shouldn't do things that way. [The order of items in `allKeys` is not defined](http://developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Classes/NSDictionary_Class/Reference/Reference.html%23//apple_ref/occ/instm/NSDictionary/allKeys). You're relying on an implementation detail that Apple are free to change at any time. – Jim Jul 02 '11 at 23:10
  • @Jim - Which I clearly articulate, as well as provide a solution. – Wayne Hartman Jul 03 '11 at 00:25
  • I'm not sure you understand the problem. It's not that the list will end up in an arbitrary order, it's that the list could have duplicate entries and missing entries. The order of `allKeys` is *undefined*, not *in an inconvenient order*. You can't assume it will be consistent from call to call. If you do things your way, you need to either sort `keys` immediately after getting it, or store `keys` in an instance variable that you will use for the lifetime of the table view. – Jim Jul 03 '11 at 00:36