gpt4 book ai didi

ios - UICollectionView 显示空单元格

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:22:08 27 4
gpt4 key购买 nike

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

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

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

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

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

也许值得一提的是我在同一 View 中也有一个 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: image 1 enter image description here enter image description here

最佳答案

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

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

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

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

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

关于ios - UICollectionView 显示空单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16499577/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com