2

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 ;-)

2 Answers 2

2

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
}
Sign up to request clarification or add additional context in comments.

3 Comments

That's beyond my experience level with iOS - this is my first project - any chance of an example?
@Dave Sure, take a look at the example and see if it makes sense.
Thanks - I've actually figured a simpler solution for my issue at the source of the data so I can flag it without having to compare anything. I'm going to mark this as the answer though as it's an important thing to know how to do for future reference.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.