UICollectionView 仅显示一个单元格 - 当视图滚动时会发生变化

UICollectionView showing only one cell - which changes when the view is scrolled

我有一个 UICollectionView,它应该显示数组中的项目。我检查了数组 - 它填充了正确数量和类型的对象。

然而,UICollectionView 仅显示一个 UICollectionView 单元格 - 当我将单元格下拉到上一个对象时它会发生变化。我以前从未见过这种行为。下面是我的代码:

(导致问题的是 SourceTankCollectionView(我假设目标集合视图​​单元格也会)

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (collectionView==self.movementsIndexCollectionView)
    {

        TransfersListCollectionViewCell* tempCell = [collectionView dequeueReusableCellWithReuseIdentifier: @"Transfer List Cell" forIndexPath:indexPath];
        if (!tempCell)
        {
            tempCell = [[TransfersListCollectionViewCell alloc] init];
        }

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

        tempCell.sourceTankLabel.text = thisMovement.sourceTank.tankNumber;
        tempCell.destinationTankLabel.text = thisMovement.destinationTank.tankNumber;
        tempCell.layer.borderWidth = 1.5;
        tempCell.layer.borderColor = [[UIColor darkGrayColor] CGColor];
        tempCell.layer.cornerRadius = 4;
        return tempCell;
    }

    else if (collectionView==self.sourceTanksCollectionView)
    {
        SourceTankCollectionViewCell* tempCell = [collectionView dequeueReusableCellWithReuseIdentifier: @"Source Tank Collection Cell" forIndexPath:indexPath];
        if (!tempCell)
        {
            tempCell = [[SourceTankCollectionViewCell alloc] init];
        }
        int index = indexPath.row;

        if (!tempCell)
        {
            tempCell = [[SourceTankCollectionViewCell alloc] init];
        }

        Tank* thisSourceTank = [self.arrayOfSourceTanks objectAtIndex:indexPath.row];
        Product* thisSourceTankProduct = thisSourceTank.tankProduct;

        tempCell.tankNumberLabel.text = thisSourceTank.tankNumber;
        tempCell.tankProductLabel.text = thisSourceTankProduct.name;
        tempCell.tankVolumeLabel.text = thisSourceTank.tankTotalVolume;
        tempCell.tankVolumeGauge.percentFilled = [thisSourceTank.tankTotalVolume doubleValue]/[thisSourceTank.tankMaxVolume doubleValue];
        //tempCell.tankVolumeToTransferLabel.text = [thisMovement.volume stringValue];
        //tempCell.tankVolumeTransferredLabel.text = @"35000";
        tempCell.transferStatusLabel.text = @"In progress";

        tempCell.backgroundColor = [UIColor darkGrayColor];
        tempCell.layer.borderWidth = 1.5;
        tempCell.layer.borderColor = [[UIColor whiteColor] CGColor];
        tempCell.layer.cornerRadius = 8;

        tempCell.frame = CGRectMake(self.sourceTanksCollectionView.frame.size.width * 0.05, self.sourceTanksCollectionView.frame.size.width * 0.05, self.sourceTanksCollectionView.frame.size.width * 0.9, 103);
        return tempCell;
    }
    else //if (collectionView==self.destinationTanksCollectionView)
    {
        DestinationTankCollectionViewCell* tempCell = [collectionView dequeueReusableCellWithReuseIdentifier: @"Destination Tank Collection Cell" forIndexPath:indexPath];
        if (!tempCell)
        {
            tempCell = [[DestinationTankCollectionViewCell alloc] init];
        }
        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.tankVolumeGauge.percentFilled = [thisDestinationTank.tankTotalVolume doubleValue]/[thisDestinationTank.tankMaxVolume doubleValue];

        tempCell.backgroundColor = [UIColor darkGrayColor];
        tempCell.layer.borderWidth = 1.5;
        tempCell.layer.borderColor = [[UIColor whiteColor] CGColor];
        tempCell.layer.cornerRadius = 8;

        tempCell.frame = CGRectMake(self.destinationTanksCollectionView.frame.size.width * 0.05, self.destinationTanksCollectionView.frame.size.width * 0.05, self.destinationTanksCollectionView.frame.size.width * 0.9, 103);
        return tempCell;
    }
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (collectionView == self.movementsIndexCollectionView)
    {
        return self.arrayOfAllMovements.count;
    }
    else if (collectionView == self.sourceTanksCollectionView)
    {
        return self.arrayOfSourceTanks.count;
    }
    else //(collectionView == self.destinationTanksCollectionView)
    {
        return self.arrayOfDestinationTanks.count;
    }
}

非常感谢您的帮助!我想这很容易解决!

您不应该设置 UICollectionViewCell 的框架。您只能设置宽度和高度。这应该在以下时间完成:

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

    return CGSizeMake(collectionView.frame.size.width/2.0, collectionView.frame.size.height/2.0);
}

据我观察,此集合视图的所有单元格都具有相同的框架矩形。也许这是导致问题的原因。所有单元格都在彼此之上。

tempCell.frame = CGRectMake(self.sourceTanksCollectionView.frame.size.width * 0.05, self.sourceTanksCollectionView.frame.size.width * 0.05, self.sourceTanksCollectionView.frame.size.width * 0.9, 103);

@hasan83 在 Swift 3 中的回答:

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    return CGSize(width: collectionView.frame.size.width / 2.0, height: collectionView.frame.size.height / 2.0)
}