Ios UICollectionView在iPad上运行时不显示单元格
我有一个UIViewController,其中包含三个UICollectionView。当我在iPhone上运行该应用程序时,该应用程序工作正常,UICollectionView会根据需要填充。 对于iPad版本,我想要的是稍微不同的行为,因此我必须给电池提供与iPhone版本不同的大小。但当我运行它时-两个动态计算尺寸的UICollectionView不显示任何单元格。我认为问题一定与此代码有关:Ios UICollectionView在iPad上运行时不显示单元格,ios,uicollectionview,uicollectionviewcell,uicollectionviewlayout,Ios,Uicollectionview,Uicollectionviewcell,Uicollectionviewlayout,我有一个UIViewController,其中包含三个UICollectionView。当我在iPhone上运行该应用程序时,该应用程序工作正常,UICollectionView会根据需要填充。 对于iPad版本,我想要的是稍微不同的行为,因此我必须给电池提供与iPhone版本不同的大小。但当我运行它时-两个动态计算尺寸的UICollectionView不显示任何单元格。我认为问题一定与此代码有关: - (CGSize)collectionView:(UICollectionView *)col
- (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;
}
}
我已尝试确保单元格的高度和宽度小于所讨论的UICollectionView,但这没有任何区别
好吧,我还是没什么好运气!UICollectionViews可以在我测试过的iPhone5和iPhone6上运行——它只是iPad版本。我希望这仍然很简单。下面是我在CollectionView中使用的其余代码-可能有一些我没有发现的东西:
- (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];
}
}
-(void)viewDidLoad{
[超级视图下载];
self.lhsGradientFillView.firstColor=[UIColor浅蓝色];
self.lhsGradientFillView.secondColor=[UIColor transparentLightBlue];
self.rhsGradientFillView.firstColor=[UIColor浅蓝色];
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.transferDirectionRow.direction=dDown;
[[UIDevice currentDevice]BegingeratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter]addObserver:自选择器:@selector(didChangeOrientation:)名称:UIDeviceOrientationDidChangeNotification对象:[UIDevice currentDevice]];
UICollectionViewFlowLayout*sourceFlowLayout=(UICollectionViewFlowLayout*)self.sourceTanksCollectionView.collectionViewLayout;
UICollectionViewFlowLayout*destinationFlowLayout=(UICollectionViewFlowLayout*)self.destinationTanksCollectionView.collectionViewLayout;
如果(习惯用法==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];
}
其他的
{
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=是;
self.sourceTanksCollectionView.paginabled=是;
self.destinationTanksCollectionView.scrollEnabled=是;
self.destinationTanksCollectionView.paginabled=YES;
self.sourceindepath=[nsindepath indexPathForRow:0,第0节];
self.destinationIndexPath=[NSIndexPath indexPathForRow:0,第0节];
self.currentBlend=[self.arrayOfBlends objectAtIndex:0];
[self.sourceTanksCollectionView设置需要显示];
[self.destinationTanksCollectionView setNeedsDisplay];
}
-(无效)ViewDidLayoutSubView
{
[超级视图布局子视图];
如果(成语!=IPAD)
{
if(self.sourceindexath.row