UICollectionViewCell滚动时显示空白单元格

时间:2018-12-21 11:53:50

标签: ios objective-c

我似乎无法弄清楚为什么在滚动或有时我解雇另一个视图控制器来查看单元格时会创建并显示额外的单元格。

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{

    NSInteger tmpIndex = indexPath.row+1; //addd 1 because userDetail is first element

    if ([self.model.order[tmpIndex] isKindOfClass:[Products class]])
    {
        Products *products = [self.model.order objectAtIndex:tmpIndex];

        if ([[products display] isEqualToString:kACTIVITY_GRID_CELL_ID])
        {
            ActivityCollectionViewGridCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kACTIVITY_GRID_CELL_ID forIndexPath:indexPath];
            cell.data = products;
            cell.delegate = self;
            cell.tag = indexPath.row;
            return cell;
        }
        else
        {
            ActivityCollectionViewListCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kACTIVITY_LIST_CELL_ID forIndexPath:indexPath];
            cell.data = products;
            cell.delegate = self;
            cell.tag = indexPath.row;

            return cell;
        }
    }
    else
    {
        OrderHeaders *orderHeaderBaskets = [self.model.order objectAtIndex:tmpIndex];
        ActivityCollectionViewListCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kACTIVITY_LIST_CELL_ID forIndexPath:indexPath];

        cell.data = orderHeaderBaskets;
        cell.delegate = self;
        cell.tag = indexPath.row;

        return cell;
    }
}

据我了解,它只为这部分代码添加了多余的空单元格:

    else
    {
        ActivityCollectionViewListCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kACTIVITY_LIST_CELL_ID forIndexPath:indexPath];
        cell.data = products;
        cell.delegate = self;
        cell.tag = indexPath.row;

        return cell;
    }
}
else
{
    OrderHeaders *orderHeaderBaskets = [self.model.order objectAtIndex:tmpIndex];

    ActivityCollectionViewListCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kACTIVITY_LIST_CELL_ID forIndexPath:indexPath];

    cell.data = orderHeaderBaskets;
    cell.delegate = self;
    cell.tag = indexPath.row;

    return cell;
} 

这可能是因为我多次使用dequeueReusableCellWithReuseIdentifier吗?还是其他原因造成的?

还有一种方法可以检查并删除多余的单元格吗?

0 个答案:

没有答案