简体   繁体   English

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

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

I have a UICollectionView which is supposed to show items from an array. 我有一个UICollectionView,它应该显示数组中的项目。 I have checked the array - and it is populated with the right number and type of objects. 我检查了数组 - 并且填充了正确的数字和对象类型。

The UICollectionView however shows only one UICollectionView cell - and it changes when I pull the cell down to the previous object. 但是,UICollectionView只显示一个UICollectionView单元格 - 当我将单元格拉到前一个对象时,它会发生变化。 I've not seen this behaviour before. 我之前没见过这种行为。 Below is my code: 以下是我的代码:

(It's the SourceTankCollectionView that is causing the problem (and I assume the destination collection view cell will too) (导致问题的是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;
    }
}

Many thanks for any help! 非常感谢您的帮助! I assume it is an easy fix! 我认为这是一个简单的解决方案!

You are not supposed to set the frame of the UICollectionViewCell. 您不应该设置UICollectionViewCell的框架。 You only can set the width and height. 您只能设置宽度和高度。 and that should be done in: 这应该在:

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

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

As I can observe all your cells for this collection view have the same frame rectangle. 我可以观察到这个集合视图的所有单元格都有相同的框架矩形。 maybe that what is causing the problem. 也许是导致问题的原因。 all the cells are on top of each other. 所有细胞都在彼此之上。

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's answer in Swift 3: @ 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)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM