【问题标题】:UICollectionView showing empty cellsUICollectionView 显示空单元格
【发布时间】:2013-05-11 16:56:10
【问题描述】:

我有一个UICollectionView,它显示了一组图像。单元格中的图像显示为白框,而不是真实图像。

我从另一个视图复制并粘贴了这段代码,它在另一个视图中运行良好......但不知何故,在另一个视图中它显示所有图像就像一个白框。

显示的方框等于应显示的图像数量。所以信息传递正确。

图像像小白框一样显示,但是当我单击它们时,它们会将所选图像显示到我放置在其他位置的另一个 UIImage 元素中(参见 image1,清除按钮左侧的绿色方块发生了变化取决于哪个白细胞Ii 触摸)。

谁能发现为什么单元格中的UIImage 显示不正确?

也许值得一提的是,我在同一个视图中也有一个 tableview

// in viewDidLoad method
[self.myCollection registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"Cells"];

//
- (void)getAllIcons
{        
    NSArray* imgArrayRaw = [[NSBundle mainBundle] pathsForResourcesOfType:@"png" inDirectory:nil];

    NSMutableArray* imgArray = [[NSMutableArray alloc] init];
    for (NSString* imgArrayProccesing in imgArrayRaw) {
        NSString* ho = [imgArrayProccesing lastPathComponent];
        if([ho rangeOfString:@"btn-"].location != NSNotFound){
            [imgArray addObject:ho];
        }
    }
    _imgFiles = [ NSMutableArray arrayWithObjects:imgArray,nil];
    _imgFiles = _imgFiles[0];


    _myCollection.hidden = NO;
    [_myCollection reloadData];
}

// Collection View
#pragma mark - UICollectionView Datasource

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{   
    return [_imgFiles count];
}

- (NSInteger)numberOfSectionsInCollectionView: (UICollectionView *)collectionView
{
    return 1;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{   
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cells" forIndexPath:indexPath];

    UIImageView *imageView = (UIImageView *)[cell viewWithTag:101]; 
    imageView.image = [UIImage imageNamed:[_imgFiles objectAtIndex:indexPath.row]]; 
    cell.backgroundColor = [UIColor whiteColor];

    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    [self changeIconImage:[_imgFiles objectAtIndex:indexPath.row]]; 
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{   
    return CGSizeMake(50, 50);  
}

- (UIEdgeInsets)collectionView:
(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{   
    return UIEdgeInsetsMake(50, 20, 50, 20);
}

图片1:

【问题讨论】:

  • 可能是因为你没有设置数据源和委托?就像使用 TableView 一样,您必须设置它们。如果不是这样,那我暂时不知道。

标签: ios objective-c cocoa-touch uiimage uicollectionview


【解决方案1】:

很抱歉,您没有提供足够的信息来完全诊断问题。我会从您的代码中怀疑以下可能性之一;

a) 你的项目中没有找到指定的图片,所以返回nil;

b) 未找到标签为 101 的 imageView,因此返回 nil;

c) 找到 imageView,但未启用或隐藏。

我会在“cell.backgroundColor”行设置一个断点并检查每个返回值并检查 imageView 的状态以查看它是否已启用而不是隐藏。

【讨论】: