netdigger
netdigger

Reputation: 3789

Data representation suited for UITableViewDataSource

Currently, I have the following set up in my app:

For example, I have a gamelist, where the games have different states and I would like a certain state to be in a certain section etc.

Since I have 3 different states I'll use a NSDictionary with State as key and Array as value and put a certain game with a certain state in the correct array.

Then my code for numberOfRowsInSection would be something like:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSString* state = [self getStateForSection:section];
    return [[obj objectForKey:state] count];
}

The problem is I dislike having the objects in arrays since then I have to look through the whole array whenever I want to access a certain match.

I would instead like to have all matches in a single NSDictionary with matchID as key and NSDictionary with as value (the match).

But how would I do in UITableView then?

I could do something where I return allKeys of my NSDictionary but I still have to iterate it to find their current state and add their key to the appropriate list?

Upvotes: 0

Views: 71

Answers (1)

Ric
Ric

Reputation: 8805

You could just also build a NSDictionary from matchID to match on the side as well, simply reusing the same objects and keeping the NSDictionary of NSArrays just for the table data source. Then the problem is that you have to update both when the data changes. You may also want to look into NSArray filteredArrayUsingPredicate:

Another possibility is using another data structure known as the sorted dictionary which keeps its keys in a specific order. That way you can ask for all the values as an array for the table view and get a constant order, but still be able to do a lookup with a matchID directly to a match.

Upvotes: 3

Related Questions