2013-08-25 78 views
-1

我在所有目录中显示图片但是它不显示图片。我将NSLog放入代码中,以便我可以找出哪些代码正在工作,并且只在日志中获得“j”。我没有在日志中看到“a”。你认为什么是错的?UICollectionView不显示图片

- (void)viewDidLoad 
    { 
     [super viewDidLoad]; 
     // Do any additional setup after loading the view. 
     allImagesArray = [[NSMutableArray alloc] init]; 
     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
     NSString *documentsDirectory = [paths objectAtIndex:0]; 
     NSArray *locations = [[NSArray alloc]initWithObjects:@"Bottoms", @"Dress", @"Coats", @"Others", @"hats", @"Tops",nil ]; 
     NSString *fPath = documentsDirectory; 
     for(NSString *component in locations) 
     { 
      fPath = [fPath stringByAppendingPathComponent:component]; 
     } 
     NSArray *directoryContent = [[NSFileManager defaultManager] directoryContentsAtPath: fPath]; 
     collectionTrash.delegate =self; 
     collectionTrash.dataSource=self; 
     for(NSString *str in directoryContent){ 
      NSLog(@"i"); 
      NSString *finalFilePath = [fPath stringByAppendingPathComponent:str]; 
      NSData *data = [NSData dataWithContentsOfFile:finalFilePath]; 
      if(data) 
      { 
       UIImage *image = [UIImage imageWithData:data]; 
       [allImagesArray addObject:image]; 
       NSLog(@"array:%@",[allImagesArray description]); 
      }} 
     for(NSString *folder in locations) { 

      // get the folder contents 

      for(NSString *file in directoryContent) { 

       // load the image 

      } 
     }} 

    - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView 
    { 
     NSLog(@"j"); 
     return 1; 
    } 

    - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section 
    { 
     return [allImagesArray count]; 


    } 


    -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath 
    { 
     static NSString *reuseID = @"ReuseID"; 
     TrashCell *mycell = (TrashCell *) [collectionView dequeueReusableCellWithReuseIdentifier:reuseID forIndexPath:indexPath]; 
     UIImageView *imageInCell = (UIImageView*)[mycell viewWithTag:1]; 
     imageInCell.image = [allImagesArray objectAtIndex:indexPath.row]; 
     NSLog(@"a"); 
     return mycell; 
    } 

随意为更多的代码。

+1

您应该修改您的原始问题,不要接受答案并提出一个新问题。 – Wain

回答

1

如果你看一下例外,它会告诉你非常精确地出了什么问题:

您正在试图调用数组,根本不存在一个“长度”的方法。您想在此处使用count。它不在你发布的代码中,因此只需要搜索length,如果项目不是很大,你很可能会发现它很容易。

NSString *fPath = [documentsDirectory stringByAppendingPathComponent:locations];给你一个警告,因为locations是一个数组。想一想 - 只是没有道理:你想添加哪些元素到路径中?

正如我想的那样,这两个错误可能都是相互关联的:你只是忽略了fPath的编译时错误 - 或警告 - 现在stringByAppendingPathComponent:方法调用其参数的长度,这是一种方法预计NSString

底线:不要忽视编译器警告!如果你修复这些问题,你可能也会减少崩溃。