如何使用字符串参数为 iPhone 表格单元格指定图像?
我有一个对象 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
放入我的 MainViewController
。我非常感谢您提供的任何帮助。
I have an object MObject
that is currently defined like so:
+ (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
is meant to contain the file name of the appropriate image anImage.png
. Once I put that in, I need the UIImage object to be created so that it can be placed in cell.imageView.image
.
I'm 1) unclear on whether the direction I've started to take (as shown above) is workable, 2) so far clueless as to how I can get this UIImage
into my in my MainViewController
. I really appreciate any help you offer.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我得到了它!我将原始帖子中显示的代码更改为:
然后我将以下行添加到
cellForRowAtIndexPath:
这就是全部!
I got it! I changed the code shown in my original post to:
Then I added the following line to
cellForRowAtIndexPath:
And that's all it took!
{
NSString *str = [[[NSBundle mainBundle]资源路径] stringByAppendingPathComponent:文件名];
UIImage *image = [[UIImage alloc] initWithContentsOfFile:str];
返回图像;
每当
你想设置图像
UIImage *img=[UIImage newImageFromResource:@"xyz.png"];
您可以在任何您想要的地方设置 img &别忘了释放img
{
NSString *str = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:filename];
UIImage *image = [[UIImage alloc] initWithContentsOfFile:str];
return image;
}
and Whenever you want to set Image
UIImage *img=[UIImage newImageFromResource:@"xyz.png"];
You can set img wherever you want & dont forget to release img