所以我得到了这些数字代码对应于一个项目,我需要获取每个项目的图像显示在表格中。 (所有的表格等已经排序,只是这个图片选择..)从for循环中的NSArray中选择图像
我到目前为止,只返回blockNotFound.png。我需要它为每个请求的'itemId'返回相应的'block-X.png'。
+ (NSImage *)imageForItemId:(uint16_t)itemId {
NSSize itemImageSize = NSMakeSize(32, 32);
NSImage *output = [[NSImage alloc] initWithSize:itemImageSize];
NSString *path = [[NSBundle mainBundle] bundlePath];
NSFileManager *fm = [NSFileManager defaultManager];
NSArray *imageArray = [fm contentsOfDirectoryAtPath:path error:nil];
for (id object in imageArray) {
NSString *imagePath = [NSString stringWithFormat:@"%@/block-%d.png",path,itemId];
// This NSLog does list all files in imagePath.
// NSLog(imagePath);
if ([fm fileExistsAtPath:imagePath]) {
output = [NSImage imageNamed:imagePath];
} else {
output = [NSImage imageNamed:@"blockNotFound.png"];
}
}
return output;
}
谢谢。
'for'循环没有意义。你不在循环中使用'object',所以你可能根本没有循环。 – 2011-04-22 12:16:13
我会用什么方法替换'object'? – Kaikz 2011-04-22 12:51:06
您需要做的第一件事是了解您的代码。你为什么首先写循环?它应该做什么?你为什么认为你需要它? – 2011-04-22 12:52:45