UICollectionView 不显示图片 [英] UICollectionView not showing pictures
问题描述
我正在显示所有目录中的图片,但是它不显示图片.我将 NSLog 放入代码中,这样我就可以找出哪些代码在工作,并且我只在日志中得到j".我在日志中没有看到a".你觉得哪里不对?
I am showing the Pictures in all of the Directories however It does not display the pictures. I am putting NSLog in the code so that I can find out which code is working and I only get "j" in the log. I do not see the "a" in the log. What do you think is wrong?
- (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;
}
请随意获取更多代码.
推荐答案
如果您查看异常,它会非常准确地告诉您问题所在:
If you look at the exception, it tells you very precisely what's wrong:
您正试图在一个根本不存在的数组上调用长度"方法.您想在此处使用 count
.不过,它不在您发布的代码中 - 所以只需搜索 length
,如果项目不大,您可能会很容易找到它.
You are trying to calling a 'length' method on an array, which simply does not exist. You want to use count
here instead. It's not in the code you posted, though - so just do a search for length
and you'll probably find it rather easily if the project isn't huge.
NSString *fPath = [documentsDirectory stringByAppendingPathComponent:locations];
给你一个警告,因为 locations
是一个数组.想一想 - 只是没有意义:你想将它的哪些元素添加到路径中?
NSString *fPath = [documentsDirectory stringByAppendingPathComponent:locations];
gives you a warning, because locations
is an array. Think about it - just doesn't make sense: which of its elements do you want to add to the path?
在我看来,这两个错误可能彼此相关:您只是忽略了 fPath 的编译时错误或警告,现在 stringByAppendingPathComponent:
方法在其参数上调用 length,这是预期的 NSString
的方法.
As I think about it, both errors probably relate to each other: You simply ignored the compile time error - or warning - for the fPath, and now the stringByAppendingPathComponent:
method calls length on its parameter, which is a method of the expected NSString
.
底线:不要忽略编译器警告!如果你解决了这些问题,你可能也会减少崩溃.
Bottom line: Do not ignore compiler warnings! If you fix those, you probably reduce crashes, too.
这篇关于UICollectionView 不显示图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!