UICollectionViewCell没有看到Image

时间:2015-11-14 23:17:14

标签: ios objective-c xcode

enter image description here在我的iPhone应用中,我有170张图片。我将这些图像添加到UICollectionView。它显示170个单元格,但它们是空的。

我的代码:

cell.imageCell.image = [UIImage imageNamed:[self.imagesArray objectAtIndex:indexPath.row]];

调试器说:

  

永远不会执行代码

UICollectionViewCell have @property * imageCell

我该如何解决这个问题?

我的代码:

   - (void) viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];

    NSMutableArray * tempArray = [[NSMutableArray alloc] init];

    for (int i = 0; i <170; i++) {
        [tempArray addObject:[UIImage imageNamed:[NSString stringWithFormat:@"%d",i]]];
    }
    self.imagesArray = [NSArray arrayWithArray:tempArray];

    for (int j = 0 ; j < [self.imagesArray count]; j++) {

        UIImageView * image =[[UIImageView alloc] init];
        image.image = [self.imagesArray objectAtIndex:j];
    }
    - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

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

    - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

    static NSString * inditifiner = @"Cell";

    MFCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:inditifiner forIndexPath:indexPath];
    return cell;


    cell.imageCell.image = [UIImage imageNamed:[self.imagesArray objectAtIndex:indexPath.row]];


    return cell;

}

2 个答案:

答案 0 :(得分:0)

您正在使用:

   cell.imageCell.image = [UIImage imageNamed:[self.imagesArray objectAtIndex:indexPath.row]];

加载图片!!但这是错误的! imageNamed函数需要字符串形式的图像名称,而不是对象!!!

对于代码从未执行过的错误,您可能在return语句之后放置了一些代码!

尝试这样做:

     cell.imageCell.image = [self.imagesArray objectAtIndex:indexPath.row];

由于图像作为对象存储在数组中,您可以直接使用上面的stmt !!!

同样在你的布局子视图方法中,你在第二次循环之后缺少关闭}。

第二个循环毫无意义

   for (int j = 0 ; j < [self.imagesArray count]; j++) {

    UIImageView * image =[[UIImageView alloc] init];  
    image.image = [self.imagesArray objectAtIndex:j];  //this doesn't show anything as image view  is not the subview of any view
    }

除了浪费内存之外,这没有任何作用!

第一个循环:

您将图像存储在名为1,2,3,...... 170的数组中,没有任何扩展名。如果您将图像存储在assets文件夹中,这样就可以了!

   for (int i = 0; i <170; i++) {
    [tempArray addObject:[UIImage imageNamed:[NSString stringWithFormat:@"%d",i]]];
}

确保您的资产中确实有170张图片名为1,2,3,...... 170。

如果他们都将png作为扩展名,并且他们不在资产中,那么你应该

    for (int i = 0; i <170; i++) {
    [tempArray addObject:[UIImage imageNamed:[NSString stringWithFormat:@"%d",i.png]]];   //if it is jpg....it should be i.jpg
}

最后请确保您正确连接imageCell的插座,以便在IB中收集视图!如果它不是插座,请确保将正确的框架正确添加到您的电池!

答案 1 :(得分:0)

  • (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

    static NSString * inditifiner = @&#34; Cell&#34 ;;

    MFCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:inditifiner forIndexPath:indexPath]; 返回单元格;

    cell.imageCell.image = [UIImage imageNamed:[self.imagesArray objectAtIndex:indexPath.row]];

    返回单元格;

}

在此方法中,您应该删除第一个返回单元格

相关问题