UICollectionView - 基于水平滚动更新数组 indexPath [英] UICollectionView - Update Array indexPath based on Horizontal Scrolling

查看:23
本文介绍了UICollectionView - 基于水平滚动更新数组 indexPath的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用两个集合单元来显示数据,子集合视图值基于主集合视图.

Hi I m using two collection cell to display data, Child Collection view values are based on main Collection View.

我有视频列表,当我选择表中的视频时,我会在索引路径处传递完整的数组,以便根据所选的数组索引路径显示主集合视图.

I have Video List and when i select Video In Table i m passing Complete array at index path, so that the main collection view will be displayed based on selected indexPath of array.

它显示正常,但是当我水平滚动它时,我传递了一个可见的单元格 indexPath 数据来获取子集合视图数据.

it is displaying fine, but when i scroll it horizontal, I an passing visible cell indexPath data to fetch child collection View Data.

但是当我滚动主集合视图时,indexPath 处的数组不会基于主集合视图更新,它始终显示从视频列表(前一个视图控制器)传递的相同 IndexPath.

But when i scroll Main Collection View , The array at indexPath is not updating based on Main Collection View it always display same IndexPath which is Passed From Video List(Previous View Controller).

如何基于水平滚动更新主集合视图数组.

How to update main collection view array based on horizontal Scrolling.

我想要这个场景-

例如视频列表数组为 1,2,3,4

我选择了 3 个索引并传递给视频列表数组的集合视图,它显示了选定的索引路径行值,当我向右滚动时,索引路径应该在左右增加到 4,对于任何值,它应该减少到 2在视频列表数组上选择.主集合视图应基于传递的索引路径工作并根据其索引路径进行操作

i select 3 index and pass to collection view for videolist array, it displayed selected index path row value, when i scroll right side the index path should get increased to 4 on right and left it should decrease to 2, for any values selected on video list array. Main Collection view should work based on passed index path and act based on its index path

我的代码-

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
 if(self.childCollectionView ==collectionView)
 {
    return [self.array_ChildElements count];
}
else
{
    return [self.array_VideoList count];
}
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
if (collectionView ==self.collectionView)
{
    VVCollectionCell *cell = (VVCollectionCell *)[self.collectionView dequeueReusableCellWithReuseIdentifier:@"VVCollectionCell" forIndexPath:indexPath];

    VVSystems *obj;
    if (self.iscalledFromVideoLIst)
    {
        obj =self.array_VideoList[self.selectedIndexPath.row];

    }
    else
    {
        obj =self.array_VideoList[indexPath.row];
    }
    [self.collectionView flashScrollIndicators];

    NSLog(@"Current Index Path--->%ld",(long)self.selectedIndexPath.row);
    NSLog(@"Current Title--->%@",obj.title);

    [cell.labelVideoTitle setText:obj.title];
    [cell.imgVideo sd_setImageWithURL:[NSURL URLWithString:obj.iconImage]
                     placeholderImage:nil];

    return cell;
}
else
{
    childCollectionCell *cell2 = (childCollectionCell *)[self.childCollectionView dequeueReusableCellWithReuseIdentifier:@"childCollectionCell" forIndexPath:indexPath];
    ChildAnimations *obj = self.array_ChildElements[indexPath.row];
    [self.childCollectionView flashScrollIndicators];

    [cell2.labelTitel setText:obj.tagName];
    [cell2.imgbackGround sd_setImageWithURL:[NSURL URLWithString:obj.image]
                     placeholderImage:nil];
    return cell2;
}
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{

for (VVCollectionCell *cell in [self.collectionView visibleCells])
{
    NSIndexPath *newIndexPath = [self.collectionView indexPathForCell:cell];


    VVSystems *objCustVehiclesList =self.array_VideoList[newIndexPath.row];

    NSLog(@"Current Index Path Row -->%ld",(long)newIndexPath.row);
    NSLog(@"Current Title--->%@",objCustVehiclesList.title);


    self.str_partID =objCustVehiclesList.partID;
    [self ServiceCallForChildVideo:self.str_partID];
    self.iscalledFromVideoLIst = NO;

}
}

推荐答案

我明白了,如果你想用 videoList 数据向左和向右滚动.

i see ,if you want to scroll both left and right with videoList datas.

正确的做法是当VC调用viewDidLoad"时,在最后一行添加:

the right way is that when the VC call "viewDidLoad", the last line add :

[self.collectionView scrollToItemAtIndexPath:self.selectedIndexPath atScrollPosition:UICollectionViewScrollPositionNone animated:NO];

并编辑代码:

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
if (collectionView ==self.collectionView)
{
    VVCollectionCell *cell = (VVCollectionCell *)[self.collectionView dequeueReusableCellWithReuseIdentifier:@"VVCollectionCell" forIndexPath:indexPath];

   // VVSystems *obj;
   // if (self.iscalledFromVideoLIst)
   // {
   //     obj =self.array_VideoList[self.selectedIndexPath.row];

   // }
   // else
   //  {
   //     obj =self.array_VideoList[indexPath.row];
   // }
    VVSystems *obj=self.array_VideoList[indexPath.row];
    [self.collectionView flashScrollIndicators];

    NSLog(@"Current Index Path--->%ld",(long)self.selectedIndexPath.row);
    NSLog(@"Current Title--->%@",obj.title);

    [cell.labelVideoTitle setText:obj.title];
    [cell.imgVideo sd_setImageWithURL:[NSURL URLWithString:obj.iconImage]
                     placeholderImage:nil];

    return cell;
}
else
{
    childCollectionCell *cell2 = (childCollectionCell *)[self.childCollectionView dequeueReusableCellWithReuseIdentifier:@"childCollectionCell" forIndexPath:indexPath];
    ChildAnimations *obj = self.array_ChildElements[indexPath.row];
    [self.childCollectionView flashScrollIndicators];

    [cell2.labelTitel setText:obj.tagName];
    [cell2.imgbackGround sd_setImageWithURL:[NSURL URLWithString:obj.image]
                     placeholderImage:nil];
    return cell2;
}
}

这篇关于UICollectionView - 基于水平滚动更新数组 indexPath的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆