2014-02-17 38 views
-1

林有問題的設置圖像到UiCollectionViewCell加載圖像到UICollectionVIewCell

這是我的代碼:

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSURL * imageURL = [NSURL URLWithString:@"https://mozorg.cdn.mozilla.net/media/img/firefox/firstrun/logo.png?2013-06"]; 
    NSData * imageData = [NSData dataWithContentsOfURL:imageURL]; 
    UIImage * image = [UIImage imageWithData:imageData]; 
    UIImageView * myImageView = [[UIImageView alloc] initWithImage:image]; 

    myImageView.image = [UIImage imageNamed:@"myimg"];//set the image 

    GLCell *cell = (GLCell *)[collectionView dequeueReusableCellWithReuseIdentifier:CELL_IDENTIFIER forIndexPath:indexPath]; 
    cell.displayString = [NSString stringWithFormat:@"%d user number", indexPath.row]; 
    cell.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"myimg"]]; 


    return cell; 
} 

字符串值顯示沒有問題,但電池沒有圖像。

+0

你確定你的圖像文件不具有擴展名?可能是它應該改爲'myImageView.image = [UIImage imageNamed:@「myimg.JPEG」]'例如? – Avt

+0

不設置myImageView.image它是覆蓋值 – codercat

+0

您的imageview不添加tableview單元格的子視圖 – codercat

回答

1
NSURL * imageURL = [NSURL URLWithString:@"https://mozorg.cdn.mozilla.net/media/img/firefox/firstrun/logo.png?2013-06"]; 
    NSData * imageData = [NSData dataWithContentsOfURL:imageURL]; 
    UIImage * image = [UIImage imageWithData:imageData]; 

    GLCell *cell = (GLCell *)[collectionView dequeueReusableCellWithReuseIdentifier:CELL_IDENTIFIER forIndexPath:indexPath]; 
    cell.displayString = [NSString stringWithFormat:@"%d user number", indexPath.row]; 
    cell.backgroundColor = [UIColor colorWithPatternImage:image]]; 
+0

感謝您的代碼,但仍然沒有apear即使我更改了url –

+0

應該檢查圖像url有效或不 – codercat

0

您並未將myImageView添加到您的單元格。而不是創建自己的UIImageView,你可以只使用的UITableViewCell的imageView財產