UICollectionView - Update Array indexPath based on

2019-09-11 05:23发布

问题:

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.

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

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.

I want ion this scenario-

For example VIdeo list array is 1,2,3,4

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

My Code-

- (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;

}
}

回答1:

They way you are finding the visible cell is correct. I doubt in your conditions here

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

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

Which might be giving you different index path. I don't know when you are setting this value self.iscalledFromVideoLIst

A different approach is to use. cell.labelVideoTitle.tag=indexPath.row; but don't put this inside the condition. And access the label title in your

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView

method.

Mainly you need to debug your method and see what is the indexPath coming the time you are scrolling, the issue is mainly here. Try to print the indexPath.row here and you will see the issue.

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath


回答2:

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

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

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

and edit the code:

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