-1

I am returning a JSON array: Array 2: ( {"checkin_ID" = 123}, {"checkin_ID" = 121;}, {"checkin_ID" = 99;}, etc)

In my code each cell has it's own checkin_ID. If the checkin_ID of the cell matches any number or string from my JSON array, I would like to show a button.

Any ideas?

Code:

ViewDidLoad:

NSError *error = nil; NSDictionary *dict2 = [[CJSONDeserializer deserializer] deserializeAsDictionary:jsonData2 error&error2]; 

if (dict2) { 
    rows2 = [dict2 objectForKey:@"likes"]; 
}

Cell Construction:

NSPredicate *checkinIDPredicate = [NSPredicate predicateWithFormat:@"checkin_ID == %d", myInt]; 

NSArray *matchingCheckins = [rows2 filteredArrayUsingPredicate:checkinIDPredicate];

if([matchingCheckins count] > 0) // Show the button, etc. 

In my NSLog after adding your code... Predicate checkin_ID = 121 Matching Predicate ()

4

1 回答 1

0

First, parse your data into an array of dictionaries, storing it in a property for later use.

NSError *error = nil;
self.checkins = [NSJSONSerialization JSONObjectWithData:checkinData options:0 error:&error];

checkinData is an NSData object, which can be created easily from your JSON string.

When the cell for the row is being constructed, filter the list of checkins by your target ID:

NSPredicate *checkinIDPredicate = [NSPredicate predicateWithFormat:@"checkin_ID == %d", cellCheckinID];
NSArray *matchingCheckins = [self.checkins filteredArrayUsingPredicate:checkinIDPredicate];
if([matchingCheckins count] > 0)
    // Show the button, etc.

This code has not been tested, so it may contain compiler or logic errors, but that's the gist of it.

于 2012-12-17T19:05:35.087 回答