将原型单元与 xcode 4 中的 plist 连接起来 [英] Connect a prototype cell with a plist in xcode 4

查看:56
本文介绍了将原型单元与 xcode 4 中的 plist 连接起来的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作我的第一个 iphone 应用程序,它是一个葡萄酒图表应用程序.它在 xcode 4.2 中带有故事板.它基于带有 4 个标签的标签栏:信息、葡萄酒图表、搜索、收藏夹.我现在正在制作葡萄酒图表,在这个选项卡中,我放置了一个带有原型单元格的 TableView,我想用葡萄酒的名称、地区和图像填充这个单元格.到目前为止,我已经这样做了:

I'm making my first iphone application and it's a wine chart application. It's with storyboard in xcode 4.2. It's based on a tab bar with 4 tabs: info, wine chart, search, favourites. I'm making the wine chart at the moment, In this tab I've put a TableView with a prototype cell and I want to fill this cell with name, district and image of the wines. So far I've done this:

1:将第二个选项卡链接到导航控制器

1: Linked the second tab to a navigation controller

2: 将导航控制器链接到 TableView (WinesViewController)

2: Linked the navigation controller to the TableView (WinesViewController)

3:创建 WinesViewController.h/.m 类

3: Created the WinesViewController.h/.m class

4:添加了一个原型单元格,标识符为:wineCell 和 title &带有文本的字幕标签:名称和区.

4: Added a prototype cell with identifier: wineCell and title & subtitle labels with text: name & district.

5: 创建一个包含所有瓶子图片 WineName.png 的文件夹

5: Made a folder with all the images of the bottles WineName.png

6:创建一个 plist:Wine.plist,每个酒有 1 个字典,字典键 = 酒名.字典包含 19 个字符串,键为名称"、地区"、图像"、酸度"等.

6: Created a plist: Wine.plist with 1 dictionary for each wine, dictionary key = wine name. The dictionaries contains 19 strings with keys "Name", "District" "Image", "Acid level" etc.

这是plist:

http://www.flickr.com/photos/80427098@N07/7372450200/in/photostream

这就是我到目前为止所做的一切,现在我想用 plist 中的字符串填充 tableview 中的单元格标签,按字母顺序按部分排序.我是新手,所以我不知道要使用哪些代码以及将它们放在哪里.有人可以帮助我更多地了解如何使用 plist 中的信息填充 tableview 吗?

That's all I've done so far, and now I want to fill the cell labels in the tableview with the strings in the plist, sorted alphabetically in sections. I'm new to this so I dont know which codes to use and where to put them. Can someone help me understand more of how to populate a tableview with information from the plist?

有人告诉我,我不应该将代码直接放在 WinesViewController.h/.m 中.然后我被告知我可以创建 NSObject 的子类(我们称之为 WineObject.h/.m),在此处放置一些 NSDictionary/NSEnumerator 代码,然后以某种方式将其链接到 WinesViewController 原型单元格.但我真的不知道怎么办!我还没有那么有经验!我真的希望有人可以帮助我了解代码,将它们放在哪里以及如何将它们链接起来.那太棒了.我现在开始有点理解 X-Code 和 Objective C 编码(终于!)但这还很先进,现在我被卡住了一些帮助会给我很多工作!也感谢您提供有用的教程、示例项目等.

I've been told that I shouldn't put the code directly in the WinesViewController.h/.m. So then I've been told that I can create a subclass of NSObject (let's call it WineObject.h/.m), put some NSDictionary / NSEnumerator codes in here, and then somehow link this to the WinesViewController prototype cell. But I really don't know how! I'm not that experienced yet! I really hope someone can give me help with the codes, where to put them and how to link it all up. That it would be fantastic. I'm starting to understand X-Code and Objective C coding a little bit now (finally!) but this is to advanced yet, and some help now that I'm stuck would give me a lot to work with! Directions to helpful tutorials, sample projects etc is also appreciated.

如果您还需要知道什么才能回答我的问题,请告诉我!我没有在 WinesViewController 或 WineObject 中写过一行代码,所以我真的需要完整的解释..

Let me know if there is something more you need to know in order to answer my question! I haven't written a single line of code in the WinesViewController or WineObject so I really need the whole explanation..

稍后我将在 DetailViewController(一个包含葡萄酒信息的全屏视图)中使用来自同一个 plist 的信息,这就是为什么当我只在 tableview 单元格中使用 3 个值时 plist 中有这么多字符串.搜索功能也将在此 plist 中的数据上进行.感谢所有有用的帮助!

Later I'll use the information from the same plist in a DetailViewController (a fullscreen view with info on the wines) thats why there's so many strings in the plist when I'm only using 3 values in the tableview cells. The search function will also be made on the data from this plist. Thank you for all useful help!

推荐答案

您应该首先阅读如何使用 model-view-controller 范式.这将解释如何将您的数据与该数据的视图分开.一旦你理解了这一点,你就能更好地理解其余的内容.

You should start by reading about how to use the model-view-controller paradigm in Objective-C. This will explain how to separate your data from views of that data. Once you understand that, you'll be in a better position to understand the rest of this.

我建议对您的 plist 文件进行一项更改.与其让顶层成为一个字典,其中每个酒的名字都是一个键,每个键的值是一个包含有关酒的信息的字典,我会让顶层是一个字典数组.每个酒都是数组中的一个元素,并且是一个字典,其中包含您当前拥有的所有信息以及酒的名称.这将更接近于您的数据模型的外观.像这样:

I would suggest making one change to your plist file. Rather than having the top level be a dictionary where each wine name is a key, and the value for each key is a dictionary containing information about the wine, I would make the top level be an array of dictionaries. Each wine would be one element in the array, and would be a dictionary containing all the information you currently have plus the wine's name. This will more closely correspond to what your data model will look like. Something like this:

<array>
    <dict>
        <key>Name</key>
        <string>J. P. Chenet Merlot</string>
        <key>Image</key>
        <string>JPChenet.png</string>
        ... the rest of the keys and values for this wine ...
    </dict>

    <dict>
        <key>Name</key>
        <string>Campo Viejo Crianza</string>
        ... the rest of the keys ...
    </dict>
</array>

每个 wine 对象将包含字典中每个元素的实例变量.(所以一个用于图像,一个用于名称,一个用于地区等)像这样:

Each wine object would contain an instance variable for each element in the dictionary. (So one for image, one for name, one for district, etc.) Something like this:

@class WineObject : NSObject { 
    NSString* name;
    NSString* imagePath;
    NSString* district;
    // ... etc.
};

你的模型可能只是一个 WineObjectsNSArray.您可以使用我在其他问题中提到的方法从 plist 中读取对象.它看起来像这样:

Your model could just be an NSArray of WineObjects. You could read the objects out of the plist using the method I mentioned in your other question. It would look something like this:

NSMutableArray* wineModel = [[NSMutableArray alloc] init];
NSArray* wines = [NSArray arrayWithContentsOfURL:<url of the plist file>];
NSEnumerator* wineEnum = [wine objectEnumerator];
NSDictionary* nextWine = nil;
while ((nextWine = [wineEnum nextObject]) != nil)
{
    // Get the wine data from the dictionary
    WineObject* newWine = [WineObject wineWithDictionary:nextWine];

    // Add the next wine to our model
    [wineModel addObject:newWine];
}

你的 WineObject 类将有一个类似这样的类初始值设定项:

Your WineObject class would then have a class initializer something like this:

+ (id)wineWithDictionary:(NSDictionary*)wineDict
{
    name = [wineDict objectForKey:@"Name"];
    district = [wineDict objectForKey:@"District"];
    imagePath = [wineDict objectForKey:@"Image"];
    // ... extract the rest of the values from the dictionary ...
}

这篇关于将原型单元与 xcode 4 中的 plist 连接起来的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆