UICollectionView - Image is getting set randomly

白昼怎懂夜的黑 提交于 2020-01-04 06:36:07

问题


I am using collectionView in my App. I am setting image for the cell backgroundView in didSelect delegate. But When i select one cell indexPath the image is getting set for 3 cell indexPath. When i scroll the collectionView the images are getting changed randomly? Please Help me. thanks in advance.

- (void)viewDidLoad
{
    [super viewDidLoad];

    [collection registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:uio];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:       (NSInteger)section
{
     return 50;
}

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

    UICollectionViewCell *cell = [collection dequeueReusableCellWithReuseIdentifier:uio
 forIndexPath:indexPath];
    cell.backgroundColor = [UIColor whiteColor];
   return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
 {
    NSLog(@"index %@",indexPath);
    UICollectionViewCell *cell = [collection cellForItemAtIndexPath:indexPath];

    cell.backgroundView =[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"download.jpg"]];

 }

回答1:


That's because you reuse your cell. An option would be to have an dictionary variable to say that your cell has been selected and reset the image if it has not been.

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"index %@",indexPath);
    UICollectionViewCell *cell = [collection cellForItemAtIndexPath:indexPath];

    cell.backgroundView =[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"download.jpg"]];

    [selectedDictionary setObject:[NSNumber numberWithBool:YES] forKey:[NSNumber numberWithInteger:indexPath.row]];
}

Then in your cellForItemAtIndexPath method you would check that value

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

    UICollectionViewCell *cell = [collection dequeueReusableCellWithReuseIdentifier:uio
 forIndexPath:indexPath];
    BOOL selected = [[selectedDictionary objectForKey:[NSNumber numberWithInteger:indexPath.row]] boolValue];

    if(selected){
        cell.backgroundView =[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"download.jpg"]];
    }else{
        cell.backgroundView = nil;
    }

    cell.backgroundColor = [UIColor whiteColor];
   return cell;
}

Of course if you use some kind of object as model, it would appropriate to have a selected variable in here, you won't need a nsdictionary any more.




回答2:


The Problem is dequeueReusableCellWithReuseIdentifier.

When you scroll UICollectionview then cell are reused that is problem add Collectionview inside scrollview.

Try this Inside:

Scroll_View is Your Scroll View

collection is Your Collectionview

-(UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    self.Scroll_View.contentSize = CGSizeMake(self.view.frame.size.width, collectionView.contentSize.height);
    CGRect fram_For_Collection_View = self.collection_view.frame;
    fram_For_Collection_View.size.height = collectionView.contentSize.height;
    self.collection.view.frame = fram_For_Collection_View;
}



回答3:


Your -collectionView:didSelectItemAtPath: is adding a new image view to the cell. Nothing is removing that image view when the cell is reused. So, when you say:

UICollectionViewCell *cell = [collection dequeueReusableCellWithReuseIdentifier:uio

forIndexPath:indexPath];

in your -collectionView:cellForItemAtIndexPath:, you're may get back some cell that already has one or more image views.

My suggestion would be to add the image view to the cell in the cell prototype, perhaps in your storyboard or in the cell's initializer. Have your -collectionView:cellForItemAtIndexPath: set the image for that image view to the correct image for the given path.




回答4:


What's happening is that UICollectionView reuses cells. So in didSelectItemAtIndexPath: you set the cell background, but then the UICollectionView reuses that same cell as needed (and you're not resetting the cell.backgroundView in cellForItemAtIndexPath:).

The way to fix this is to maintain an NSIndexSet of selected cells. In didSelectItemAtIndexPath: you can add the index of the item that was selected, and then force a reload of that item by calling reloadItemsAtIndexPaths. Then, in your cellForItemAtIndexPath: check the index set to see if the selected index is included, and if so, set the backgroundView of the cell.




回答5:


I had the same issue few days ago & I posted a question here. Here is the answer I got & it works for me.

Collection View Cell multiple item select Error

And also if you are using a custom cell you can add this code to the init method of that cell & it will work too.

        CGFloat borderWidth = 6.0f;
        UIView *bgView = [[UIView alloc] initWithFrame:frame];
        bgView.layer.borderColor = [UIColor redColor].CGColor;
        bgView.layer.borderWidth = borderWidth;
        self.selectedBackgroundView = bgView;


来源:https://stackoverflow.com/questions/23013935/uicollectionview-image-is-getting-set-randomly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!