将图像加载到UICollectionVIewCell中

时间:2014-02-17 11:23:10

标签: ios

我有问题将图像设置为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;
}

字符串值显示没有问题,但单元格没有图像。

2 个答案:

答案 0 :(得分: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]];

答案 1 :(得分:0)

您没有将myImageView添加到您的单元格。您可以只使用UITableViewCell的imageView属性

,而不是创建自己的UIImageView。
相关问题