2013-05-11 125 views
3

我有一個顯示圖像數組的UICollectionView。單元格中的圖像顯示爲白色框而不是實際圖像。UICollectionView顯示空單元格

我複製並從另一種觀點認爲將這些代碼,並在其他視圖的偉大工程......但不知何故在這個其它查看其顯示像一個白盒的所有圖像。

所示包裝盒是等於應該顯示的圖像的數量。所以信息正確傳遞。

圖像像小白盒一樣顯示,但是當我點擊它們時,它們會將選中的圖像顯示到另一個UIImage元素中,我放置在其他位置(請參閱image1,清除按鈕左側的綠色方塊會根據在哪個白細胞上觸摸)。

任何人都可以發現爲什麼在單元格中UIImage無法正常顯示?

也許是值得metioning,我也有同樣觀點的人的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); 
} 

此搜索: image 1 enter image description here enter image description here

+0

可能因爲你沒有設置數據源和委託?就像你使用TableView一樣,你必須設置它們。如果情況並非如此,那麼我現在不知道。 – 2013-05-11 17:45:48

回答

1

對不起,您沒有提供足夠的信息來完全診斷問題。我會懷疑你的代碼有以下可能性之一;

一)指定的圖像尚未在你的項目中找到,所以又回到零; b)帶tag 101的imageView沒有找到,所以返回nil;

三)ImageView的被發現,但未啓用,或者被隱藏。

我將在「cell.backgroundColor」行設置一個斷點,並檢查每一個返回值和檢查的ImageView的狀態,看到它被啓用,而不是隱藏。

相關問題