如何使用字符串参数指定iPhone表格单元格的图像?

时间:2010-11-12 05:31:16

标签: objective-c uitableview nsstring uiimage

我有一个对象MObject,目前定义如下:

+ (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放入my {MainViewController 1}}。非常感谢您提供的任何帮助。

2 个答案:

答案 0 :(得分:1)

我明白了!我将原始帖子中显示的代码更改为:

+ (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];

这就是全部!

答案 1 :(得分:0)

  • (UIImage *)newImageFromResource:(NSString *)filename {
    NSString * str = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:filename]; UIImage * image = [[UIImage alloc] initWithContentsOfFile:str]; 返回图像; }

并且每当您想要设置图像

UIImage * img = [UIImage newImageFromResource:@“xyz.png”];

您可以在任何地方设置img&别忘了发布img