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

时间:2015-08-16 19:53:51

标签: 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;
    }
}

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

2 个答案:

答案 0 :(得分:6)

您不应该设置UICollectionViewCell的帧。您只能设置宽度和高度。这应该在:

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);

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

$(document).ready()

答案 1 :(得分:0)

@ 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)
}