0

I have a UIImageView installed on my cell, it is transparent black, but when I scroll the CollectionView and I raise my UIImageView but there is always more to transprence. See picture :

1 - I is not even scroll I is not even scroll

2 - After a scroll After a scroll

My code :

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

    static NSString *CellIdentifier = @"pictureCell";

    MSContestListCollectionViewCell *cell = (MSContestListCollectionViewCell *)[self.collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];

    cell.titleContest.adjustsFontSizeToFitWidth = YES;
    cell.titleContest.minimumScaleFactor = 0.5;

    cell.pictureImageView.layer.cornerRadius = 5;
    cell.pictureImageView.clipsToBounds = YES;

    cell.titleView.layer.cornerRadius = 5;
    cell.titleView.clipsToBounds = YES;

    switch (_segmentedControl.selectedSegmentIndex) {
        case 0: {
            NSDictionary *searchResult = [self.readArray objectAtIndex:indexPath.item];
            NSString *stringImage = [searchResult objectForKey:@"featuredImage"];

            NSString *image = [NSString stringWithFormat:@"https://srv.mediaswapp.com/%@", stringImage];
            [cell.pictureImageView sd_setImageWithURL:[NSURL URLWithString:image]
                                     placeholderImage:[UIImage imageNamed:@"placeholder.png"]];

            cell.statusContest.text = [searchResult objectForKey:@"status"];

            if ([[searchResult objectForKey:@"status"] isEqualToString:@"PAUSE"]) {
                cell.titleContest.text = [NSString stringWithFormat:@"Concours en pause"];
                /*
                UIView *view = [[UIView alloc] initWithFrame:CGRectMake(cell.contentView.frame.origin.x, cell.contentView.frame.origin.y, cell.contentView.frame.size.width, cell.contentView.frame.size.height)];
                view.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.8f];
                view.layer.cornerRadius = 5;
                view.clipsToBounds = YES;
                [cell.contentView addSubview:view];
                 */
                UIImageView *imagecellPause = [[UIImageView alloc] initWithFrame:CGRectMake(cell.contentView.frame.origin.x, cell.contentView.frame.origin.y, cell.contentView.frame.size.width, cell.contentView.frame.size.height)];
                UIImage *cellImage = [UIImage imageNamed:@"cell-pause.png"];
                imagecellPause.image = cellImage;
                [cell.contentView addSubview:imagecellPause];
                UIImageView *imagePause = [[UIImageView alloc] initWithFrame:CGRectMake(69.5, 69.5, 25, 25)];
                UIImage *image = [UIImage imageNamed:@"Pause Filled-50 (1).png"];
                imagePause.image = image;
                [cell.contentView addSubview:imagePause];
                UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 90, 150, 50)];
                label.textColor = [UIColor whiteColor];
                label.font = [UIFont fontWithName:@"Avenir-Book" size:12];
                label.text = [searchResult objectForKey:@"description"];
                label.adjustsFontSizeToFitWidth = YES;
                label.minimumScaleFactor = 0.5;
                [cell.contentView addSubview:label];
                UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(10, 105, 150, 50)];
                label2.textColor = [UIColor whiteColor];
                label2.font = [UIFont fontWithName:@"Avenir-Black" size:15];
                label2.text = @"Concours en pause";
                [cell.contentView addSubview:label2];
                cell.titleView.hidden = YES;
            } else {
                cell.titleContest.text = [searchResult objectForKey:@"description"];
            }
            break;
        }
        case 1: {
            NSDictionary *searchResult2 = [self.readArrayWinner objectAtIndex:indexPath.item];
            NSString *stringImage = [searchResult2 objectForKey:@"featuredImage"];

            NSString *image = [NSString stringWithFormat:@"https://srv.mediaswapp.com/%@", stringImage];
            [cell.pictureImageView sd_setImageWithURL:[NSURL URLWithString:image]
                                     placeholderImage:[UIImage imageNamed:@"placeholder.png"]];

            cell.titleContest.text = [searchResult2 objectForKey:@"description"];
            cell.statusContest.text = [searchResult2 objectForKey:@"status"];

            NSLog(@"le gagnant : %@", [searchResult2 valueForKeyPath:@"winners.name"]);
            break;
        }
        case 2: {
            NSDictionary *searchResult3 = [self.readArrayPhotos objectAtIndex:indexPath.item];
            NSString *stringImage = [searchResult3 objectForKey:@"featuredImage"];

            NSString *image = [NSString stringWithFormat:@"https://srv.mediaswapp.com/%@", stringImage];
            [cell.pictureImageView sd_setImageWithURL:[NSURL URLWithString:image]
                                     placeholderImage:[UIImage imageNamed:@"placeholder.png"]];

            cell.titleContest.text = [searchResult3 objectForKey:@"description"];
            cell.statusContest.text = [searchResult3 objectForKey:@"status"];
            break;
        }

        default:
            break;
    }

    [_activity stopAnimating];
    _label1.hidden = YES;

    return cell;
}
victor bill
  • 209
  • 2
  • 15

1 Answers1

0

Your CellForRowAtIndexPath should never contain addSubView, since everytime the cell is reloaded it will add the view. Thats why it keeps getting darker, it just adds view onto view.

Instead you should subclass your UICollectionViewCell and add the view in the subclass.

Black Magic
  • 2,706
  • 5
  • 35
  • 58
  • Okay thank you, and in my subclasses, according to what I have to add my subview ? – victor bill Feb 02 '16 at 09:59
  • 1
    Actually, it's OK to add a subview here. But you need to check whether the subview has already been added first. You can use view tags for that. For example, give the image view a tag 42 and then you can do `UIImageView* imageView = [cell viewWithTag:42]; if (!imageView) { /* Create image view and add it */ }` – DarkDust Feb 02 '16 at 10:04
  • this is the same, my code : UIImageView* imageView = [cell viewWithTag:42]; if (!imageView) { imageView = [[UIImageView alloc] initWithFrame:CGRectMake(cell.contentView.frame.origin.x, cell.contentView.frame.origin.y, cell.contentView.frame.size.width, cell.contentView.frame.size.height)]; UIImage *cellImage = [UIImage imageNamed:@"cell-pause.png"]; imageView.image = cellImage; [cell.contentView addSubview:imageView]; } – victor bill Feb 02 '16 at 10:15
  • @victorbill: You also need to set the tag when you create the image view: `imageView.tag = 42;`. – DarkDust Feb 02 '16 at 10:41