Question

So i work with UICollectionView with my JSON. I write this code for parse, but i am not understand for why it's not work. My JSON have ([jsontest2] after key "imageMain").

So i paste my code, please help me:

   @property (nonatomic,strong) NSMutableArray *patternImagesArray;

@end

@implementation ViewController

@synthesize patternImagesArray = _patternImagesArray;

NSURLConnection *connection;
NSMutableData *webdata;

Try to get data from JSON:

-(void) viewDidLoad{

    _patternImagesArray = [[NSMutableArray alloc] init];

    NSURL *url = [NSURL URLWithString:@"site"];


    NSURLRequest *request = [NSURLRequest requestWithURL:url];

    connection = [NSURLConnection connectionWithRequest:request delegate:self];
    if (connection) {
        webdata = [[NSMutableData alloc]init];

    }

}


-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    [webdata setLength:0];
}

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [webdata appendData:data];
}

-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{


}

JSON parse:

-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSDictionary *allDataDictionary = [NSJSONSerialization JSONObjectWithData:webdata options:0 error:nil];
    NSArray *tmp =[allDataDictionary objectForKey:@"jsontest2"];
    if (tmp.count>0){

        for (NSDictionary *diction in tmp) {
            [self.patternImagesArray addObject:diction];
        }


        NSLog(@"%@", self.patternImagesArray);


    }
    [self.collectionView reloadData];
}

Some customize things:

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

    PatternView *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"PatternCell" forIndexPath:indexPath];

    NSString *myPatternString = [[self.patternImagesArray objectAtIndex:indexPath.row] valueForKey:@"thumb"];
    NSLog(@"myPatternString %@",myPatternString);
    NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:myPatternString]];
    cell.patternImageView.image = [UIImage imageWithData:data];


    return cell;


}

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return [self.patternImagesArray count];
}



-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{

    return CGSizeMake(100.0, 100.0);
}
Was it helpful?

Solution

You are abusing the variable "array" for two totally different purposes.

The underlying reason is that you haven't bothered to find reasonable names for variables. "array" is actually a global variable. It's not only accessible from any code within your file, but from any code anywhere in your program. Look at this bit of code:

NSDictionary *allDataDictionary =[NSJSONSerialization JSONObjectWithData:_webdata 
                                          options:kNilOptions  error:nil];

array =[allDataDictionary objectForKey:@"jsontest2"];
for (array in allDataDictionary) {
}

You first parse the JSON data to allDataDictionary (you don't bother checking for errors). You access one key "jsontest2" from the dictionary and store it into the global variable "array". Whatever was in "array" before is now gone.

Then you have a for-loop, using the same global variable as the loop variable! That's total nonsense. But you also need to realise that this will iterate over the keys in your dictionary, so array is now an NSString and not an array anymore.

After that your code gets worse...

Go through your code line by line. Every line, think about what it is doing. There are many severe problems there.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top