简体   繁体   English

集合视图在滚动时更改选定的单元格

[英]Collection View changes the selected cell on scrolling

I am trying to select a cell in UICollectionView , it gets selected but on scroll down it selects the some other cell on the bottom and scroll up it shows some other to be selected. 我试图在UICollectionView选择一个单元格,但它被选中,但是在向下滚动时,它选择了底部的其他单元格,向上滚动时,它显示了要选择的其他单元格。

Below is the code which I am using didSelectItemAtIndexPath 下面是我使用didSelectItemAtIndexPath的代码

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{ 
    NSIndexPath *newIndex = indexPath;

    cell = (CustomCollectionCell *)[collectionView cellForItemAtIndexPath:newIndex];

    NSString *strId = [[masterArray valueForKey:@"id"]objectAtIndex:indexPath.row];


    NSString *tempIndexRow = [NSString stringWithFormat:@"%ld",(long)indexPath.row];

    NSLog(@"%@, %@,%d ,%@, %d", strId,tempIndexRow,cell.imageView.tag,[boolArray objectAtIndex:indexPath.row],indexPath.row);

    if (strId && [[boolArray objectAtIndex:indexPath.row] isEqualToString:@"False"] && cell.imageView.tag == indexPath.row) {

        cell.selectedImage.image = [UIImage imageNamed:@"select.png"];

        [boolArray replaceObjectAtIndex:indexPath.row withObject:@"True"];
    }
    else{
        cell.selectedImage.image = Nil;

        [boolArray replaceObjectAtIndex:indexPath.row withObject:@"False"];
    }
}

This is what I select for the first time 这是我第一次选择

在此处输入图片说明

This is what I get when I scroll down 这就是我向下滚动时得到的

在此处输入图片说明

Thanks 谢谢

You need to set selected item index store in to one array and at cellForRowIndex time check this array index with indexPath.row like bellow:- 您需要将选定的项目索引存储设置为一个数组,并在cellForRowIndex时像下面这样用indexPath.row检查该数组索引:-

selectedCellsArray alloc this array at ViewDIdLoad method selectedCellsArrayViewDIdLoad方法中ViewDIdLoad此数组

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
     cell = (CustomCollectionCell *)[collectionView cellForItemAtIndexPath:newIndex];
     if ( [selectedCellsArray containsObject:[NSString stringWithFormat:@"%d",indexPath.row]]  )
            {
                [selectedCellsArray removeObject:[NSString stringWithFormat:@"%d",indexPath.row]];
                cell.selectedImage.image = Nil;


            }
            else
            {
                [selectedCellsArray addObject:[NSString stringWithFormat:@"%d",indexPath.row]];
                cell.selectedImage.image = [UIImage imageNamed:@"select.png"];
            }
}

and

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
        NSNumber *rowNsNum = [NSNumber numberWithUnsignedInt:indexPath.row];
        if ( [selectedCellsArray containsObject:[NSString stringWithFormat:@"%@",rowNsNum]]  )
        {
           cell.selectedImage.image = [UIImage imageNamed:@"select.png"];


        }
        else
        {
            cell.selectedImage.image = Nil;
        }
}

It is not selecting another cell. 它没有选择另一个单元格。 Problem is mostlikely due to you reusing cells and not reintializing them correctly when they come back on the screen. 问题很可能是由于您重新使用单元格,并且当它们再次出现在屏幕上时,它们没有正确地重新初始化。 When Cells not visible, it unloaded to save memory. 当“单元”不可见时,将其卸载以节省内存。

So better check this condtion as well in cellForItemAtIndexPath datasource 因此最好在cellForItemAtIndexPath数据源中检查此条件

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

     {

      //You cell initialization code. 

      if ([[boolArray objectAtIndex:indexPath.row] isEqualToString:@"True"] ) {

            cell.selectedImage.image = [UIImage imageNamed:@"select.png"];
        }
        else{
            cell.selectedImage.image = Nil;
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM