从plist中读取图像名称语法问题?

发布于 2024-11-27 05:07:09 字数 2118 浏览 1 评论 0原文

我正在尝试从 plist 中读取对图像文件的引用,并使图像显示在详细视图中。虽然我可以为我的表视图布局执行此操作,但我在使用 DetailViewController nib 文件执行此操作时遇到困难。我希望有人能帮助我解决问题并向我解释哪里出了问题。代码后面是我对我认为我正在做的事情的解释。如果您能向我解释我做错了什么以及告诉我我应该做什么,我将非常感激。我一边努力学习,一边努力让事情顺利进行。

我认为问题出在两个地方。第一个是我的 didSelectRowAtIndexPath 方法:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //Get the dictionary of the selected data source.
    NSDictionary *dictionary = [[[self.localSites objectAtIndex:indexPath.section]objectForKey:@"rowData"] objectAtIndex:indexPath.row];

    TableDetailViewController *controller = [[TableDetailViewController alloc] initWithNibName:@"TableDetailViewController" bundle:[NSBundle mainBundle]];  

    controller.title = [dictionary objectForKey:@"Location"];
    controller.dText = [dictionary objectForKey:@"details"];

    // THIS IS WHERE THE PROBLEM IS
    NSString *tiny = [dictionary objectForKey:@"thumb"];
    controller.mainImage  = [UIImage imageNamed:tiny];

    [self.navigationController pushViewController:controller animated:YES];

    [controller release];
}

在上面标记为 //THIS IS WHERE I THINK THE PROBLEM IS 的代码中,我想做的是获取对图像包含在 plist 中的字符串中,并使用引用来获取图像。这种方法在我的 cellForRowAtIndexPath 方法中运行良好,我使用它用我的 plist 引用的缩略图填充表格的左侧。

在上面的代码中 mainImage 指的是我尝试通过我的 连接到 TableDetailViewController.xib 中的 UIImageView 出口的字符串TableDetailViewController.m 文件如下。这就是我认为第二个问题的地方:

- (void)viewWillAppear:(BOOL)animated 
{
    [super viewDidLoad];

    //This Bit says that the text contained in UITextView blurb is the string dText, which is used in my didSelectRowAtIndex method
    blurb.text = dText;

    //This is where I try to do the same thing by saying that UIImageView topImage is the NSString mainImage which I use in my didSelectRowAtIndex method
    topImage.image =  mainImage;    
}

我的逻辑是 topImage 是 UIImageView , mainImage 是包含图像视图的字符串,在我的 didSelectRowAtIndex 方法中,我传递了 plist 文件中包含的键“thumb”的值。

然而,上面的内容引发了警告。有人可以帮忙吗?

感谢您抽出时间阅读。

I'm trying to read a reference to an image file out of a plist and make the image display in a detail view. although I can do this for my table view layout, I'm having trouble doing this with my DetailViewController nib file. I'm hoping someone can help me fix the problem and explain to me where I'm going wrong. What follows with the code is my explanation of what I THINK I'm doing. If you could explain to me what I'm doing wrong as much as show me what I should be doing, I'd really appreciate it. I'm trying to learn as much as trying to make things work.

I think the problem resides in two places. The first is my didSelectRowAtIndexPath method:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //Get the dictionary of the selected data source.
    NSDictionary *dictionary = [[[self.localSites objectAtIndex:indexPath.section]objectForKey:@"rowData"] objectAtIndex:indexPath.row];

    TableDetailViewController *controller = [[TableDetailViewController alloc] initWithNibName:@"TableDetailViewController" bundle:[NSBundle mainBundle]];  

    controller.title = [dictionary objectForKey:@"Location"];
    controller.dText = [dictionary objectForKey:@"details"];

    // THIS IS WHERE THE PROBLEM IS
    NSString *tiny = [dictionary objectForKey:@"thumb"];
    controller.mainImage  = [UIImage imageNamed:tiny];

    [self.navigationController pushViewController:controller animated:YES];

    [controller release];
}

In the code above marked //THIS IS WHERE I THINK THE PROBLEM IS, What I'm trying to do is grab the reference to the image contained in string in the plist and use the reference to get the image. This approach works fine in my cellForRowAtIndexPath method where I use it to populate the left hand side of the table with thumbnail images referred to from my plist.

In the above code mainImage refers to a string I've tried to connect to a UIImageView outlet in my TableDetailViewController.xib through my TableDetailViewController.m file as below. This is where I think Problem number two is:

- (void)viewWillAppear:(BOOL)animated 
{
    [super viewDidLoad];

    //This Bit says that the text contained in UITextView blurb is the string dText, which is used in my didSelectRowAtIndex method
    blurb.text = dText;

    //This is where I try to do the same thing by saying that UIImageView topImage is the NSString mainImage which I use in my didSelectRowAtIndex method
    topImage.image =  mainImage;    
}

My Logic was that topImage is a UIImageView and mainImage is the string containing the image view which in my didSelectRowAtIndex method I pass the value of the key 'thumb' contained in my plist file.

However the above throws up warnings. Can anyone help?

Thanks for taking the time to read.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

将军与妓 2024-12-04 05:07:09

好的,我设法解决了这个问题。我的错误是将指针 mainImage 声明为 NSString 而不是 UIImage。我这样做是因为在 .plist 中我使用的图像名称包含在字符串中。我意识到,当我在 didSelectRowAtIndexPath 方法中将此字符串更改为同名的 .png 文件时,当我将其传递给 mainImage 时,它​​不再是我传递的字符串,而是图像。已排序!对于像我这样的新手来说,这是一个很容易犯的错误。我想我应该发帖,因为我确信我不会是唯一犯这个错误的人。

Ok, I managed to work this out. My mistake was to declare the pointer mainImage as a NSString instead of as a UIImage. I did this because in the .plist I am using the name of the image is contained within a string. I realised though that as I change this string for the .png file of the same name in my didSelectRowAtIndexPath method, by the time I'm passing it to mainImage it is no longer a string I'm passing, it's an image. Sorted! An easy mistake for a newb like me to make. Thought I should post as I'm sure I wont be the only one who makes this mistake.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文