Reputation: 493
I have 2 arrays. The first is an array of dictionaries from a JSON feed. The second is just an array of values I'm using to populate a UITableView.
For each row in the table I need to know whether the value I'm writing to the table cell exists in the first array in any of the dictionaries, so I can set the accessory type appropriately. Is there a simple way of doing this, or would I be better off creating another array of the id values from each dictionary first?
I hope that makes sense, can't see any other way to explain it ;-)
Upvotes: 2
Views: 1824
Reputation: 726509
You can use indexesOfObjectsPassingTest:
with a block that searches inside the dictionary, but it is suboptimal: you will be re-doing the same search over and over for each cell, which might slow down your app, especially when the user scrolls through your table quickly. You would be a lot better off creating an NSSet
with the items as soon as you get the feed, and re-use that when you render your table cells.
Example:
Construct NSSet *allIds
when you receive your JSON feed, like this:
allIds = [[NSMutableSet alloc] init]; // NSSet *allIds is declared
// in the same class as the array from which you get your table cells
[responseArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
[allIds addObjectsFromArray:[arrayOfDictsFromJsonFeed allKeys]];
}];
When you need to decided whether or not to show your accessory view, do this:
if ([allIds containsObject:currentId]) {
// Add accessory view
}
Upvotes: 2
Reputation: 100581
You should add your values to the dictionary if they don't exist and link to a NSNull
object so you can check this whenever you need to display a cell.
This will be much faster than checking on every scroll
Upvotes: 0