2010-11-12 21 views
0

我有一個是目前像這樣定義的對象MObject如何使用字符串參數爲iPhone表格單元格指定圖像?

+ (id)objectWithType:(NSString*)type name:(NSString*)name code:(NSString*)code imageName:(NSString*)imageName 
{ 
UIImage *theImage = [[UIImage alloc] imageNamed:imageName]; 
MObject *newObject = [[[self alloc] init] autorelease]; 
newObject.type = type; 
newObject.name = name; 
newObject.code = code; 
return newObject; 
} 

imageName應該是包含適當的圖像anImage.png的文件名。一旦我把它放入,我需要創建UIImage對象,以便它可以放置在cell.imageView.image

我1)不清楚我開始採取的方向(如上所示)是否可行,2)目前爲止我一直無法知道如何將這個UIImage加入我的MainViewController中。我非常感謝你提供的任何幫助。

回答

1

我得到了它,您可以設置IMG!我改變了我原來的職位所示的代碼:

+ (id)objectWithType:(NSString*)type name:(NSString*)name code:(NSString*)code imageName:(NSString*)imageName 
{ 
MObject *newObject = [[[self alloc] init] autorelease]; 
newObject.type = type; 
newObject.name = name; 
newObject.code = code; 
newObject.imageName = imageName; 
return newObject; 
} 

然後,我添加以下行cellForRowAtIndexPath:

cell.imageView.image=[UIImage imagedNamed:object.imageName]; 

而這一切,花了!

0
  • (的UIImage *)newImageFromResource:(的NSString *)文件名 {
    的NSString *海峽= [[[一個NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:文件名]; UIImage * image = [[UIImage alloc] initWithContentsOfFile:str]; return image; }

,只要你想設置圖片

的UIImage * IMG = [UIImage的newImageFromResource:@ 「xyz.png」];

,無論你想&不要忘記釋放IMG

+0

你能解釋一下這地方應該在我的代碼去?什麼是替代?我很難理解你的答案。 – scojac 2010-11-14 04:00:31

相關問題