这是视频效果:
列表滚动
需求滑动上面,下面选中对应,选中下面,上面滚动到对应位置
上一个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;
}
}