UICollectionViews在iPad上运行时不显示单元格

时间:2016-01-18 05:32:12

标签: ios uicollectionview uicollectionviewcell uicollectionviewlayout

我有一个UIViewController,里面有三个UICollectionViews。当我在iPhone上运行应用程序时,应用程序运行正常,并根据需要填充UICollectionViews。 对于iPad版本,我想要稍微不同的行为,因此我必须给单元格相对于父版本而不是iPhone版本。当我运行它时 - 具有动态计算尺寸的两个UICollectionViews不显示任何单元格。我认为问题必须与此代码一起使用:

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{

if (IDIOM != IPAD)
{
    if (collectionView == self.sourceTanksCollectionView)
    {
        return CGSizeMake(self.sourceTanksCollectionView.frame.size.width, self.sourceTanksCollectionView.frame.size.height * 0.95);
    }
    else if (collectionView == self.destinationTanksCollectionView)
    {
        return CGSizeMake(self.destinationTanksCollectionView.frame.size.width, self.destinationTanksCollectionView.frame.size.height* 0.95);
    }
    else
    {
        return CGSizeMake(50,60);
    }
}
else
{
    CGSize iPadCellSize;
    if (collectionView == self.sourceTanksCollectionView)
    {
        iPadCellSize = CGSizeMake(self.sourceTanksCollectionView.frame.size.width, (self.sourceTanksCollectionView.frame.size.height/self.arrayOfSourceTanks.count));
    }
    else if (collectionView == self.destinationTanksCollectionView)
    {
        iPadCellSize = CGSizeMake(self.destinationTanksCollectionView.frame.size.width, (self.destinationTanksCollectionView.frame.size.height/self.arrayOfDestinationTanks.count));
    }
    else
    {
        iPadCellSize = CGSizeMake(50,60);
    }
    return iPadCellSize;
}
}

我已经尝试确保单元格的高度和宽度小于所讨论的UICollectionViews,但这并没有任何区别。

好的 - 我还没有运气好! UICollectionViews可以在我测试的iPhone5和iPhone6上运行 - 它只是iPad版本。我希望这仍然很简单。这是我用于集合视图的其余代码 - 也许有些东西我没有发现:

- (void)viewDidLoad {
    [super viewDidLoad];
    self.lhsGradientFillView.firstColor = [UIColor lightBlue];
    self.lhsGradientFillView.secondColor = [UIColor transparentLightBlue];
    self.rhsGradientFillView.firstColor = [UIColor lightBlue];
    self.rhsGradientFillView.secondColor = [UIColor transparentLightBlue];
    self.lhsGradientFillView.gradientDirection = dLeftToRight;
    self.rhsGradientFillView.gradientDirection = dRightToLeft;
    self.currentSelectedMovement = 0;
    self.movementsIndexCollectionView.delegate = self;
    self.movementsIndexCollectionView.dataSource = self;
    self.sourceTanksCollectionView.delegate = self;
    self.sourceTanksCollectionView.dataSource = self;
    self.destinationTanksCollectionView.delegate = self;
    self.destinationTanksCollectionView.dataSource = self;

    self.blendDataProvider = [[BlendDataProvider alloc] init];
    self.blendDataProvider.dataDelegate = self;
    [self.blendDataProvider getArrayOfBlends];

    self.movementIndicatorArrow.direction = dUp;
    self.transferDirectionArrow.direction = dDown;

    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didChangeOrientation:) name:UIDeviceOrientationDidChangeNotification object:[UIDevice currentDevice]];

    UICollectionViewFlowLayout* sourceFlowLayout = (UICollectionViewFlowLayout*)self.sourceTanksCollectionView.collectionViewLayout;
    UICollectionViewFlowLayout* destinationFlowLayout = (UICollectionViewFlowLayout*)self.destinationTanksCollectionView.collectionViewLayout;

    if (IDIOM==IPAD)
    {
        UICollectionViewFlowLayout* srcLayout = (UICollectionViewFlowLayout*)self.sourceTanksCollectionView.collectionViewLayout;
        srcLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
        UICollectionViewFlowLayout* destLayout = (UICollectionViewFlowLayout*)self.destinationTanksCollectionView.collectionViewLayout;
        destLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
        sourceFlowLayout.itemSize = CGSizeMake(self.sourceTanksCollectionView.frame.size.width, self.sourceTanksCollectionView.frame.size.height/2);
        destinationFlowLayout.itemSize = CGSizeMake(self.destinationTanksCollectionView.frame.size.width, self.destinationTanksCollectionView.frame.size.height/2);
        [self.sourceTanksCollectionView.collectionViewLayout invalidateLayout];
        [self.destinationTanksCollectionView.collectionViewLayout invalidateLayout];
    }
    else
    {
        UICollectionViewFlowLayout* srcLayout = (UICollectionViewFlowLayout*)self.sourceTanksCollectionView.collectionViewLayout;
        srcLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        UICollectionViewFlowLayout* destLayout = (UICollectionViewFlowLayout*)self.destinationTanksCollectionView.collectionViewLayout;
        destLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        sourceFlowLayout.itemSize = CGSizeMake(self.sourceTanksCollectionView.frame.size.width, self.sourceTanksCollectionView.frame.size.height/2);
        destinationFlowLayout.itemSize = CGSizeMake(self.destinationTanksCollectionView.frame.size.width, self.destinationTanksCollectionView.frame.size.height/2);
    }
    self.sourceTanksCollectionView.scrollEnabled = YES;
    self.sourceTanksCollectionView.pagingEnabled = YES;
    self.destinationTanksCollectionView.scrollEnabled = YES;
    self.destinationTanksCollectionView.pagingEnabled = YES;
    self.sourceIndexPath = [NSIndexPath indexPathForRow:0 inSection:0];
    self.destinationIndexPath = [NSIndexPath indexPathForRow:0 inSection:0];
    self.currentBlend = [self.arrayOfBlends objectAtIndex:0];
    [self.sourceTanksCollectionView setNeedsDisplay];
    [self.destinationTanksCollectionView setNeedsDisplay];


}

- (void)viewDidLayoutSubviews
{

    [super viewDidLayoutSubviews];

    if (IDIOM!=IPAD)
    {
        if (self.sourceIndexPath.row < self.arrayOfSourceTanks.count)
        {
            [UIView animateWithDuration:0 animations: ^{[self.sourceTanksCollectionView reloadData]; }
                             completion:^(BOOL finished){;
                 [self.sourceTanksCollectionView scrollToItemAtIndexPath:self.sourceIndexPath atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:NO];
             }];
            self.sourceTanksCollectionViewPageControl.currentPage = self.sourceIndexPath.row;
        }

        if (self.destinationIndexPath.row < self.arrayOfDestinationTanks.count)
        {
            [UIView animateWithDuration:0 animations: ^{[self.destinationTanksCollectionView reloadData];}
                             completion:^(BOOL finished){;
                 [self.destinationTanksCollectionView scrollToItemAtIndexPath:self.destinationIndexPath atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:NO];
             }];
            self.destinationTanksColelctionViewPageControl.currentPage = self.destinationIndexPath.row;
        }
        [self.movementsIndexCollectionView setNeedsDisplay];
    }
    else
    {
        self.sourceTanksCollectionViewPageControl.currentPage = self.sourceIndexPath.row;
        self.destinationTanksColelctionViewPageControl.currentPage = self.destinationIndexPath.row;
        [self.movementsIndexCollectionView setNeedsDisplay];
        [self.sourceTanksCollectionView reloadData];
        [self.destinationTanksCollectionView reloadData];
    }
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if ([collectionView isEqual:self.movementsIndexCollectionView])
    {
        if ([[self.arrayOfAllMovements objectAtIndex:indexPath.row] isKindOfClass:[Movement class]])
        {
            TransfersListCollectionViewCell* tempCell = [collectionView dequeueReusableCellWithReuseIdentifier: @"Transfer List Cell" forIndexPath:indexPath];

            Movement* thisMovement = [self.arrayOfAllMovements objectAtIndex:indexPath.item];

            tempCell.sourceTankLabel.text = thisMovement.sourceTank.tankNumber;
            [tempCell bringSubviewToFront:tempCell.sourceTankLabel];
            tempCell.destinationTankLabel.text = thisMovement.destinationTank.tankNumber;

            tempCell.tankGauge.percentFilled = [thisMovement.transferredVolume doubleValue]/[thisMovement.totalVolume doubleValue];
            tempCell.tankGauge.lightFillColor = [UIColor lightestBlue];
            tempCell.tankGauge.darkFillColor = [UIColor midBlue];

            [tempCell sendSubviewToBack:tempCell.tankGauge];
            [tempCell.tankGauge setNeedsDisplay];
            [tempCell setNeedsDisplay];
            return tempCell;
        }
        else
        {

        }
    }

    else if ([collectionView isEqual:self.sourceTanksCollectionView])
    {
        SourceTankCollectionViewCell* tempCell = [collectionView dequeueReusableCellWithReuseIdentifier: @"Source Tank Collection Cell" forIndexPath:indexPath];
        Tank* thisSourceTank = [self.arrayOfSourceTanks objectAtIndex:indexPath.row];
        Product* thisSourceTankProduct = thisSourceTank.tankProduct;
        Movement* thisMovement = [self.currentBlend.movements objectAtIndex:indexPath.row];

        tempCell.tankNumberLabel.text = thisSourceTank.tankNumber;
        tempCell.tankProductLabel.text = thisSourceTankProduct.name;
        tempCell.tankVolumeLabel.text = thisSourceTank.tankTotalVolume;
        tempCell.percentageFilled = [NSNumber numberWithDouble: [thisSourceTank.tankTotalVolume doubleValue]/[thisSourceTank.tankMaxVolume doubleValue]];
        tempCell.tankVolumeToTransferLabel.text = [thisMovement.totalVolume stringValue];
        tempCell.tankVolumeTransferredLabel.text = [thisMovement.transferredVolume stringValue];
        tempCell.transferStatusLabel.text = thisMovement.status;
        if ([thisMovement.status isEqualToString:@"In Progress"])
        {
            tempCell.transferStatusLabel.layer.masksToBounds = YES;
            tempCell.transferStatusLabel.layer.cornerRadius = 8;
            tempCell.transferStatusLabel.backgroundColor = [UIColor darkBlue];
            tempCell.transferStatusLabel.textColor = [UIColor lightBlue];
        }
        else
        {
            tempCell.transferStatusLabel.layer.masksToBounds = NO;
            tempCell.transferStatusLabel.layer.cornerRadius = 0;
            tempCell.transferStatusLabel.backgroundColor = [UIColor lightBlue];
            tempCell.transferStatusLabel.textColor = [UIColor darkBlue];
        }
        tempCell.backgroundColor = [UIColor lightBlue];
        tempCell.layer.borderWidth = 1.5;
        tempCell.layer.borderColor = [[UIColor darkBlue] CGColor];
        tempCell.layer.cornerRadius = 8;
        [tempCell setNeedsDisplay];
        return tempCell;
    }

    else if ([collectionView isEqual:self.destinationTanksCollectionView])
    {
        DestinationTankCollectionViewCell* tempCell = [collectionView dequeueReusableCellWithReuseIdentifier: @"Destination Tank Collection Cell" forIndexPath:indexPath];
        Tank* thisDestinationTank = [self.arrayOfDestinationTanks objectAtIndex:indexPath.item];
        Product* thisDestinationTankProduct = thisDestinationTank.tankProduct;

        tempCell.tankNumberLabel.text = thisDestinationTank.tankNumber;
        tempCell.tankProductLabel.text = thisDestinationTankProduct.name;
        tempCell.tankVolumeLabel.text = thisDestinationTank.tankTotalVolume;
        tempCell.percentageFilled = [NSNumber numberWithDouble:[thisDestinationTank.tankTotalVolume doubleValue]/[thisDestinationTank.tankMaxVolume doubleValue]];

        tempCell.backgroundColor = [UIColor lightBlue];
        tempCell.layer.borderWidth = 1.5;
        tempCell.layer.borderColor = [[UIColor darkBlue] CGColor];
        tempCell.layer.cornerRadius = 8;
        [tempCell setNeedsDisplay];
        return tempCell;
    }
    return nil;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{

    if (IDIOM != IPAD)
    {
        if (collectionView == self.sourceTanksCollectionView)
        {
            return CGSizeMake(self.sourceTanksCollectionView.frame.size.width, self.sourceTanksCollectionView.frame.size.height * 0.95);
        }
        else if (collectionView == self.destinationTanksCollectionView)
        {
            return CGSizeMake(self.destinationTanksCollectionView.frame.size.width, self.destinationTanksCollectionView.frame.size.height* 0.95);
        }
        else
        {
            return CGSizeMake(50,60);
        }
    }
    else
    {
        CGSize iPadCellSize;
        if (collectionView == self.sourceTanksCollectionView)
        {
            //iPadCellSize = CGSizeMake(self.sourceTanksCollectionView.frame.size.width/4, (self.sourceTanksCollectionView.frame.size.height/self.arrayOfSourceTanks.count)/4);
            iPadCellSize = CGSizeMake(0, 0);
        }
        else if (collectionView == self.destinationTanksCollectionView)
        {
            //iPadCellSize = CGSizeMake(self.destinationTanksCollectionView.frame.size.width/4, (self.destinationTanksCollectionView.frame.size.height/self.arrayOfDestinationTanks.count)/4);
            iPadCellSize = CGSizeMake(0, 0);
        }
        else
        {
            iPadCellSize = CGSizeMake(50,60);
        }
        return iPadCellSize;
    }
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    double xInset;
    UIEdgeInsets inset;
    if ([collectionView isEqual:self.movementsIndexCollectionView])
    {
        xInset = (self.movementsIndexCollectionView.frame.size.width/2) - 25;
        inset = UIEdgeInsetsMake(0, xInset, 0, xInset);

    }
    return inset;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
        //we need to make sure that the cell that has been selected has a transfer associated with it!
    if (collectionView == self.movementsIndexCollectionView && [[collectionView cellForItemAtIndexPath:indexPath] isKindOfClass:[TransfersListCollectionViewCell class]])
    {

        NSIndexPath* previousCurrentMovement = [NSIndexPath indexPathForItem:self.currentSelectedMovement inSection:0];
        self.currentSelectedMovement = indexPath.row;
        self.currentMovement = [self.arrayOfAllMovements objectAtIndex:indexPath.row];

        Movement* thisMovement = [self.arrayOfAllMovements objectAtIndex:indexPath.row];
        for (Blend* thisBlend in self.arrayOfBlends)
        {
            if (thisBlend.dbId == thisMovement.blendId)
            {
                self.currentBlend = thisBlend;
                [self loadSourceTanksInToArray];
                [self loadDestinationTanksInToArray];

                for (int i=0; i<self.arrayOfSourceTanks.count; i++)
                {
                    Tank* srcTank = [self.arrayOfSourceTanks objectAtIndex:i];
                    if ([thisMovement.sourceTank isEqual:srcTank])
                    {
                        self.sourceIndexPath = [NSIndexPath indexPathForRow:i inSection:0];
                    }
                }

                for (int i=0; i<self.arrayOfDestinationTanks.count; i++)
                {
                    Tank* destTank = [self.arrayOfDestinationTanks objectAtIndex:i];
                    if ([thisMovement.destinationTank isEqual:destTank])
                    {
                        self.destinationIndexPath = [NSIndexPath indexPathForRow:i inSection:0];
                    }
                }
            }
        }
        [self.movementsIndexCollectionView deselectItemAtIndexPath:previousCurrentMovement animated:NO];
        [self.movementsIndexCollectionView selectItemAtIndexPath:indexPath animated:YES scrollPosition:UICollectionViewScrollPositionCenteredHorizontally];
        [self.movementsIndexCollectionView setNeedsDisplay];
        [self viewDidLayoutSubviews];
    }
}

0 个答案:

没有答案