自动适应所有UICollectionViewCell从数据源到屏幕而不滚动

时间:2014-11-13 07:51:34

标签: ios objective-c uicollectionview uicollectionviewcell

我的数据源中有一系列UIColor,我想要做的就是适应所有细胞'大小,所以他们都可以在屏幕上看到而不滚动。

到目前为止,我还不知道该怎么做:

#pragma mark <UICollectionViewDataSource>

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

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

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];

    // Configure the cell
    UIColor *cellColor = (UIColor *)self.dataSource[indexPath.row];
    cell.backgroundColor = cellColor;

    for (UIView *view in cell.contentView.subviews) {
        if ([view isKindOfClass:[UIImageView class]]) {
            if ([[(UIImageView *)view image] isEqual:[UIImage imageNamed:@"V_BG.png"]]) {
                [view removeFromSuperview];
            }
        }
    }

    if ([cellColor isEqual:self.existingColor]) {
        UIImageView *img = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"V_BG.png"]];
        img.frame = CGRectMake(0, 0, 30, 30);
        img.center = cell.contentView.center;
        img.backgroundColor = [UIColor clearColor];
        [cell.contentView addSubview:img];
    }

    return cell;
}

#pragma mark <UICollectionViewDelegate>

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{    
    [[NSNotificationCenter defaultCenter] postNotificationName:UIColorDidChangeNotification
                                                        object:[NSString stringWithFormat:@"%ld", (long)indexPath.row]];

    [self.navigationController popViewControllerRetro];
}

#pragma mark Collection view layout things

// Layout: Set cell size
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat fixSize = ([UIScreen mainScreen].bounds.size.width / 3) - 2;
    return CGSizeMake(fixSize, fixSize);
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 2;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 2;
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(0, 0, 0, 0);
}

0 个答案:

没有答案