Md. Tariqul Islam Md. Tariqul Islam - 3 days ago 5
Objective-C Question

Multiple UICollectionView in one view update second UICollectionView data

I want to design as like image. I use two UICollectionView to design. But I can't update second UICollectionView data after click fast on UICollectionview row. I never think this is the correct way to do this. Any suggestion please. I need to do with Objective-C.

enter image description here

My Code:

- (NSInteger)collectionView:(UICollectionView *)view numberOfItemsInSection:(NSInteger)section
{
if (view.tag == 1) {
return 20;//SelfiThemeIm;age.count
} else {
return 15;
}
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
if (cv.tag == 0) {
TopCell *cell = [cv dequeueReusableCellWithReuseIdentifier:@"TopCell" forIndexPath:indexPath];
NSData * imageData = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString: SelfiThemeImage[indexPath.row]]];
cell.imageView.image = [UIImage imageWithData: imageData];

return cell;
} else {
BottomCell *cell = [cv dequeueReusableCellWithReuseIdentifier:@"BottomCell" forIndexPath:indexPath];

NSData * imageData = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString: SelfiThemePhysicalFileName[indexPath.row]]];
cell.imageView.image = [UIImage imageWithData: imageData];

return cell;
}
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
NSString *ThemeURL;

if (collectionView.tag == 1) {
ThemeURL = @"http://wap.shabox.mobi/sticker_app_server/content.aspx?catid=";
ThemeURL =[ThemeURL stringByAppendingString:[SelfiThemeImageContentCode objectAtIndex:indexPath.row]];

BottomCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"BottomCell" forIndexPath:indexPath];
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager GET:ThemeURL
parameters:nil
success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSString *array= [responseObject objectForKey:@"stickers"];

self.selfidata = array;

// NSLog(@"JSON responce Selfi: %@", self.selfidata);

for(int i=0; i<=self.selfidata.count-1; i++){
NSString *PhysicalFileName = @"http://wap.shabox.mobi/CMS/content/graphics/Stickers/D200x200/";
PhysicalFileName =[PhysicalFileName stringByAppendingString:[[self.selfidata objectAtIndex:i]objectForKey:@"PhysicalFileName"]];
PhysicalFileName =[PhysicalFileName stringByAppendingString:@".png"];

[SelfiThemePhysicalFileName addObject:[NSString stringWithFormat:PhysicalFileName]];
NSLog(@"PhysicalFileName Array - %@", PhysicalFileName);
}

NSLog(@"SelfiThemePhysicalFileName Array - %@", SelfiThemePhysicalFileName);

NSData * imageData = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString: SelfiThemePhysicalFileName[indexPath.row]]];
cell.imageView.image = [UIImage imageWithData: imageData];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
}else{
}
}

Answer
- (NSInteger)collectionView:(UICollectionView *)view numberOfItemsInSection:(NSInteger)section
{
    if (view.tag == 1) {
        return SelfiThemeImage.count;
    } else {
        return SelfiThemePhysicalFileName.count;
    }
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (cv.tag == 0) {
        TopCell *cell = [cv dequeueReusableCellWithReuseIdentifier:@"TopCell" forIndexPath:indexPath];
        NSData * imageData = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString: SelfiThemeImage[indexPath.row]]];
        cell.imageView.image  = [UIImage imageWithData: imageData];

        return cell;
    } else {
        BottomCell *cell = [cv dequeueReusableCellWithReuseIdentifier:@"BottomCell" forIndexPath:indexPath];

        NSData * imageData = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString: SelfiThemePhysicalFileName[indexPath.row]]];
        cell.imageView.image  = [UIImage imageWithData: imageData];

        return cell;
    }
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *ThemeURL;

    if (collectionView.tag == 1) {
        ThemeURL = @"http://wap.shabox.mobi/sticker_app_server/content.aspx?catid=";
        ThemeURL =[ThemeURL stringByAppendingString:[SelfiThemeImageContentCode objectAtIndex:indexPath.row]];

        BottomCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"BottomCell" forIndexPath:indexPath];
        AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
        [manager GET:ThemeURL
          parameters:nil
             success:^(AFHTTPRequestOperation *operation, id responseObject) {
                 NSString *array= [responseObject objectForKey:@"stickers"];

                 self.selfidata = array;

                 // NSLog(@"JSON responce Selfi: %@",     self.selfidata);

                 for(int i=0; i<=self.selfidata.count-1; i++){
                     NSString *PhysicalFileName = @"http://wap.shabox.mobi/CMS/content/graphics/Stickers/D200x200/";
                     PhysicalFileName =[PhysicalFileName stringByAppendingString:[[self.selfidata objectAtIndex:i]objectForKey:@"PhysicalFileName"]];
                     PhysicalFileName =[PhysicalFileName stringByAppendingString:@".png"];

                     [SelfiThemePhysicalFileName addObject:[NSString stringWithFormat:PhysicalFileName]];
                     NSLog(@"PhysicalFileName Array - %@", PhysicalFileName);
                 }

                 NSLog(@"SelfiThemePhysicalFileName Array - %@", SelfiThemePhysicalFileName);

                [self.myCollectionView reloadData];
             } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                 NSLog(@"Error: %@", error);
             }];
    }else{
    }
}

I made changes in your code that just replace it with your code .

Happy Coding. give vote and approve it

Thank You.

Comments