两个个UICollectionView滚动和选中

在这里插入图片描述

这是视频效果:

列表滚动

需求滑动上面,下面选中对应,选中下面,上面滚动到对应位置

上一个UICollectionView,下面一个UICollectionView,上面的CollectionView的只有一个section,下面的CollectionView有n个section,上面的数据源个数bannerArray等于下面的的数据源个数之和,数据格式一样。我测试的的json数据:
{
    "data": [
        {
            "date": "SO JSON在线",
            "message": [
                {
                    "id": "11",
                    "selected": 1,
                    "bannerSelected": 1,
                    "name": "ICP备案查询",
                    "url": "https://icp.sojson.com"
                },
                {
                    "id": "12",
                    "selected": 0,
                    "name": "JSON在线解析",
                    "url": "https://www.sojson.com"
                },
                {
                    "id": "13",
                    "selected": 0,
                    "name": "房贷计算器",
                    "url": "https://fang.sojson.com"
                }
            ]
        },
        {
            "date": "SO JSON在线",
            "message": [
                {
                    "id": "14",
                    "selected": 0,
                    "name": "ICP备案查询",
                    "url": "https://icp.sojson.com"
                }
            ]
        },
        {
            "date": "SO JSON在线",
            "message": [
                {
                    "id": "16",
                    "selected": 0,
                    "name": "ICP备案查询",
                    "url": "https://icp.sojson.com"
                },
                {
                    "id": "17",
                    "selected": 0,
                    "name": "JSON在线解析",
                    "url": "https://www.sojson.com"
                },
                {
                    "id": "18",
                    "selected": 0,
                    "name": "房贷计算器",
                    "url": "https://fang.sojson.com"
                },
                {
                    "id": "1qaxrhferfh8343",
                    "selected": 0,
                    "name": "房贷计算器",
                    "url": "https://fang.sojson.com"
                },
                {
                    "id": "2wsxerhferfh8343",
                    "selected": 0,
                    "name": "房贷计算器",
                    "url": "https://fang.sojson.com"
                }
            ]
        }
    ]
}


第一线选中下面算法:
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    [collectionView deselectItemAtIndexPath:indexPath animated:YES];
    if (collectionView == self.bannerCollectionView) {
        return;
    }
    __block NSInteger index =  indexPath.item;
    [self.data.data enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        DateTimerSectionModel *sectionModel = (DateTimerSectionModel *)obj;
        
        [sectionModel.message enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            DateTimerItemModel *itemModel = (DateTimerItemModel *)obj;
            itemModel.selected =  [NSNumber numberWithBool:NO].stringValue;
        }];
        if (indexPath.section > idx) {
            index = index + sectionModel.message.count;
        } else if (indexPath.section == idx) {
            DateTimerItemModel *itemModel = sectionModel.message[indexPath.item];
            itemModel.selected = [NSNumber numberWithBool:YES].stringValue;
        }
    }];

    
    [self.bannerCollectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:index inSection:0] atScrollPosition:UICollectionViewScrollPositionNone animated:YES];
    [self.bannerCollectionView reloadData];
    [self.contentCollectionView reloadData];
}
再滚动上面:
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context {
    if ([keyPath isEqualToString:@"currentIndex"]) {
        
//        NSInteger oldIndex = [change[@"old"] integerValue];
        NSInteger newIndex = [change[@"new"] integerValue];
        
        /*
         *  newIndex == 0   row == 0   section == 0
         *  newIndex == 1   row == 1   section == 0
         *  newIndex == 2   row == 2   section == 0
         *  newIndex == 3   row == 0   section == 1
         *  第二组比第一组少的情况 两种算法:
         *  第一:
         *  if (row >= sectionModel.message.count ) {
         *      row = row - sectionModel.message.count;
         *      section = section + 1;
         *   } else {
         *      *stop = YES;
         *   }
         *  第二:
         *  if (row >= sectionModel.message.count && section == idx) {
         *      row = row - sectionModel.message.count;
         *      section = section + 1;
         *   }
         */
        __block  NSInteger section = 0;
        __block  NSInteger row = newIndex;
        [self.data.data enumerateObjectsUsingBlock:^(DateTimerSectionModel *  _Nonnull sectionModel, NSUInteger idx, BOOL * _Nonnull stop) {
            
            if (row >= sectionModel.message.count ) {
                row = row - sectionModel.message.count;
                section = section + 1;
            } else {
                *stop = YES;
            }
            [sectionModel.message enumerateObjectsUsingBlock:^(DateTimerItemModel *  _Nonnull itemModel, NSUInteger idx, BOOL * _Nonnull stop) {
                itemModel.selected =  [NSNumber numberWithBool:NO].stringValue;
            }];
        }];
        NSLog(@"section=%lu---row=%lu",section,row);
        DateTimerSectionModel *sectionModel = self.data.data[section];
        DateTimerItemModel *itemModel1 = sectionModel.message[row];
        itemModel1.selected = [NSNumber numberWithBool:YES].stringValue;
        
//        NSIndexPath *topIndexPath = [NSIndexPath indexPathForRow:newIndex inSection:0];

        NSIndexPath *bottomIndexPath = [NSIndexPath indexPathForRow:row inSection:section];
//    atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally
//        [self.bannerCollectionView scrollToItemAtIndexPath:topIndexPath atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];
//        [self.bannerCollectionView reloadData];
        
        [self.contentCollectionView scrollToItemAtIndexPath:bottomIndexPath atScrollPosition:UICollectionViewScrollPositionNone animated:YES];
        
        [self.contentCollectionView reloadData];
        
        return;
        
    }
}
demo放在GitHub上面了.
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值