Anonymous Anonymous - 9 days ago
71 0

No description

Objective-C

CollectionView

- (void)setupCollectionView {
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;

    self.stickersSize = [[NSMutableArray alloc] init];
    self.cellToResize = [[NSMutableDictionary alloc] init];
    self.eraseCollectionViewCount = 0;
    self.cellSpacing = 2.0f;

    for(int currentStickerIndex = 0; currentStickerIndex < [self.promoStickersArray count]; currentStickerIndex++) {
        
        SVZSticker *currentSticker = self.promoStickersArray[currentStickerIndex];
        CGSize cellSize;
        cellSize.height = 20.0f;
        cellSize.width = [currentSticker.title sizeWithAttributes:NULL].width + 5.0f;
        
        CGFloat spaceLeft = [self spaceLeftinRowforItem:currentStickerIndex];
        if(cellSize.width > spaceLeft) {
            CGSize previousCellSize = [self.stickersSize[currentStickerIndex - 1] CGSizeValue];
            
            NSValue *previousCellSizeValue = [NSValue valueWithCGSize:previousCellSize];
            [self.cellToResize setObject:previousCellSizeValue forKey:[NSString stringWithFormat:@"%d", currentStickerIndex - 1]];
            
            previousCellSize.width += spaceLeft + self.cellSpacing;
            self.stickersSize[currentStickerIndex - 1] = [NSValue valueWithCGSize:previousCellSize];
            self.collectionViewHeightConstraint.constant += 21.0f;
            self.eraseCollectionViewCount ++;
        	
            self.firstItemInRow = currentStickerIndex;
        }
        if(currentStickerIndex == [self.promoStickersArray count] - 1) {
            spaceLeft = [self spaceLeftinRowforItem:currentStickerIndex];
            
            NSValue *cellSizeValue = [NSValue valueWithCGSize:cellSize];
            [self.cellToResize setObject:cellSizeValue forKey:[NSString stringWithFormat:@"%d", currentStickerIndex]];
            
            cellSize.width += (spaceLeft - cellSize.width);
        }
        [self.stickersSize insertObject:[NSValue valueWithCGSize:cellSize] atIndex:currentStickerIndex];
    }
    
    self.collectionCellIdentifier = [SVZPromoStickerCollectionViewCell reuseIdentifier];
    [self.collectionView registerNib:[UINib nibWithNibName:self.collectionCellIdentifier bundle:nil] forCellWithReuseIdentifier:self.collectionCellIdentifier];
}

- (CGFloat)spaceLeftinRowforItem:(NSInteger)itemIndex
{
    CGRect screenRect = [[UIScreen mainScreen] bounds];
    CGFloat spaceLeft = screenRect.size.width - self.leadingCollectionViewConstraint.constant - self.trailingCollectionViewConstraint.constant;
    for(NSInteger stickerIndex = self.firstItemInRow; stickerIndex < itemIndex; stickerIndex ++) {
        SVZSticker *stickerInRow = self.promoStickersArray[stickerIndex];
        CGFloat width = [stickerInRow.title sizeWithAttributes:NULL].width + 5.0f;
        spaceLeft = spaceLeft - width - self.cellSpacing;
    }
    return spaceLeft;
}

#pragma mark - UICollectionViewDataSource

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return [self.promoStickersArray count];
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    SVZPromoStickerCollectionViewCell *cell = [self.collectionView dequeueReusableCellWithReuseIdentifier:self.collectionCellIdentifier
                                                                                                        forIndexPath:indexPath];
    NSString *indexPathString = [NSString stringWithFormat:@"%d", indexPath.row];
    if([self.cellToResize objectForKey:indexPathString]) {
        CGSize trueCellSize = [self.cellToResize[indexPathString] CGSizeValue];
        cell.labelTrailingSpaceConstraint.constant += (cell.width - trueCellSize.width);
    }
    NSInteger currentStickerIndex = indexPath.row;
    SVZSticker *currentSticker = self.promoStickersArray[currentStickerIndex];

    cell.promoLabel.backgroundColor = currentSticker.backgroundColor;
    cell.promoLabel.text = currentSticker.title;
    
    return cell;
}

#pragma mark - UICollectionViewDelegateFlowLayout

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    return [self.stickersSize[indexPath.row] CGSizeValue];
}