所以我得到了這些數字代碼對應於一個項目,我需要獲取每個項目的圖像顯示在表格中。 (所有的表格等已經排序,只是這個圖片選擇..)從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