Вопрос

I have a Collection View & I want to select more than one item. For that I'm using

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    [self.selectedAsset addObject:self.assets[indexPath.row]];

    UICollectionViewCell* cell=[self.collectionView cellForItemAtIndexPath:indexPath];
    cell.contentView.backgroundColor = [UIColor blackColor];
}

This method to add objects to selectedAsset NSMutableArray.

This is the cellForItemAtIndexPath method.

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

    Cell *cell = [cv dequeueReusableCellWithReuseIdentifier:@"MY_CELL" forIndexPath:indexPath];

    // load the asset for this cell
    ALAsset *asset = self.assets[indexPath.row];
    CGImageRef thumbnailImageRef = [asset thumbnail];
    UIImage *thumbnail = [UIImage imageWithCGImage:thumbnailImageRef];

    // apply the image to the cell
    cell.imageView.image = thumbnail;
    [cell.label removeFromSuperview];
    //cell.imageView.contentMode = UIViewContentModeScaleToFill;

    return cell;
}

I use this code to chance the background colour of the cell.

UICollectionViewCell* cell=[self.collectionView cellForItemAtIndexPath:indexPath];
cell.contentView.backgroundColor = [UIColor blackColor];

But when I select 1st item in the Collection View, both 1st & 15th item in the Collection View change the background Colour.

Why is this happen? Please can someone give me a solution.

Это было полезно?

Решение

Ok, here's what seems to be your problem.

1) When you select first cell this method is called

    - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath

In this method you change cell background colour to be black, so this cell is black from now on.

2) You scroll down and new cells are loaded with method

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

There is one tricky line in implementation

dequeueReusableCellWithReuseIdentifier:

So for new cell your app will likely to not create new cell but to show one that is not visible, for example cell #1, which you selected at beginning, and which got black background colour.

So for new cell your app might reuse old one which might be modified.

My fix for you would be next -

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

UICollectionViewCell *cell = [cv dequeueReusableCellWithReuseIdentifier:@"MY_CELL" forIndexPath:indexPath];

//line below might not work, you have to tune it for your logic, this BOOL needs to return weather cell with indexPath is selected or not
BOOL isCellSelected = [self.selectedAsset containsObject:self.assets[indexPath.row]];
if(!isCellSelected)
{
    UICollectionViewCell* cell=[cv cellForItemAtIndexPath:indexPath];
    cell.contentView.backgroundColor = [UIColor clearColor]; // or whatever is default for your cells
}

// load the asset for this cell
ALAsset *asset = self.assets[indexPath.row];
CGImageRef thumbnailImageRef = [asset thumbnail];
UIImage *thumbnail = [UIImage imageWithCGImage:thumbnailImageRef];

// apply the image to the cell
cell.imageView.image = thumbnail;
[cell.label removeFromSuperview];
//cell.imageView.contentMode = UIViewContentModeScaleToFill;

return cell;
}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top