【发布时间】:2014-02-15 16:38:28
【问题描述】:
我的资源文件夹中有一个图像列表,我想将其加载到集合视图中
我在文档目录中的NSAarray 中有我的图像文件名。
.h
{
NSArray *fileList;
NSString * searchedImage;
}
.m
NSFileManager *filesm = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSArray *getFilelist= [filesm contentsOfDirectoryAtPath:[paths objectAtIndex:0] error:nil];
BOOL isDirectory;
for (searchedImage in getFilelist){
BOOL fileExistsAtPath = [[NSFileManager defaultManager] fileExistsAtPath:searchedImage isDirectory:&isDirectory];
if (fileExistsAtPath) {
if (isDirectory)
{
//Found Directory
}
}
if ([[searchedImage pathExtension] isEqualToString:@"png"]) {
//This is Image File with .png Extension
NSLog(@"directoryContents = %@",searchedImage);
}
}
//count files
NSFileManager *fm = [NSFileManager defaultManager];
NSArray *pathsAmount = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
fileList= [fm contentsOfDirectoryAtPath:[pathsAmount objectAtIndex:0] error:nil];
int filesCount = [fileList count];
NSLog(@"filesCount:%d", filesCount);
UINib *cellNib = [UINib nibWithNibName:@"NibCell" bundle:nil];
[self.appliancesCollectionView registerNib:cellNib forCellWithReuseIdentifier:@"cvCell"];
}
return self;
}
这会在viewDidLoad 中正确地将我的图像名称重新命名为63293446.png、appliance63293447.png、appliance63293448.png、appliance63293449.png 等
我想将这些加载到我的收藏视图单元格中。目前它在-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath 方法中为我的图像文件名返回一个空值
除了在这里研究之外,我到目前为止所做的尝试
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return fileList.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = @"cvCell";
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
cell.backgroundView = [[UIImageView alloc] initWithImage: [UIImage imageNamed: [NSString stringWithFormat:@"%@",searchedImage]]];
NSLog(@"cell Bg image %@",searchedImage); <-----------------//returns null
return cell;
}
【问题讨论】:
-
这令人困惑。你说你的数组是'searchedImage',然后你用它作为一个字符串来创建图像。你说你得到了有效的名字(我假设'searchedImage'包含文件名)然后你说它返回null。你能澄清一点吗?
-
什么是
searchedImage?你是说你的日志里有cell Bg image null吗? -
如果您显示填充
searchedImage的代码及其定义方式,将会有所帮助。 -
请将您的代码粘贴到您正在初始化 fileList 和 searchedImage 的位置。
-
代码已更新,@Akshat 是的
标签: ios objective-c cocoa-touch uicollectionview