一个视图中的多个UICollectionView更新第二个UICollectionView数据

时间:2016-11-29 05:00:15

标签: ios objective-c uicollectionview

我想设计像图像一样。我用两个UICollectionView来设计。但是在UICollectionview行上快速单击后,我无法更新第二个UICollectionView数据。我从不认为这是正确的方法。有任何建议请。我需要使用Objective-C。

enter image description here

我的代码:

- (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{
    }
}

4 个答案:

答案 0 :(得分:1)

  • 您需要有两个引用CollectioView的对象,例如_collectionView1_collectionView1

  • 现在确保将这些2的委托设置为自我

  • -(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath检查

    if ([collectionView isEqualTo:_collectionView1]) {
        _collectionView2DataSource = ;//Do your calculation and setting
        [_collectionView2 reloadData] ;
    }
    

我正在考虑您已经填充了初始值,并且您知道所有集合视图数据源和委托功能。

答案 1 :(得分:1)

- (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{
    }
}

我对您的代码进行了更改,只需将其替换为您的代码。

快乐编码。投票并批准

谢谢。

答案 2 :(得分:1)

  1. 如果您在storyboard / xib文件中有第二个集合视图,请为该集合视图创建IBOUTLET并将其命名为&#34; what_ever_name_you_want&#34;

  2. 如果您以编程方式创建了这个,请将集合视图作为类中的全局变量

  3. didSelect方法中的
  4. 通过调用["what_ever_name_you_gave" reloadData];重新加载集合视图

答案 3 :(得分:0)

您需要更改此行,因为此行会创建新的单元格实例。

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

改变:

BottomCell *cell = [collectionView cellForItemAtIndexPath:indexPath];