UICollectionView仅显示一个单元格 - 在滚动视图时会更改

Hil*_*ich 3 ios uicollectionview uicollectionviewcell

我有一个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;
    }
}
Run Code Online (Sandbox Code Playgroud)

非常感谢您的帮助!我认为这是一个简单的解决方案!

has*_*san 6

您不应该设置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);
}
Run Code Online (Sandbox Code Playgroud)

我可以观察到这个集合视图的所有单元格都有相同的框架矩形.也许是导致问题的原因.所有细胞都在彼此之上.

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);
Run Code Online (Sandbox Code Playgroud)