从Plist读取信息以在TableView中使用 [英] Reading information from a Plist to use in TableView

查看:90
本文介绍了从Plist读取信息以在TableView中使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我检查了其他帖子,但似乎没有回答我的问题,所以我去:)
我有一个Plist,内容如下:


I've checked the other posts, but non seem to answer my question, so here I go :)
I've got a Plist, with the following contents:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>001</key>
    <dict>
        <key>Name</key>
        <string>Name1</string>
        <key>Number</key>
        <string>001</string>
    </dict>
    <key>002</key>
    <dict>
        <key>Name</key>
        <string>Name2</string>
        <key>Number</key>
        <string>002</string>
    </dict>
</dict>
</plist>

我正在尝试将此代码转换为数组并用于TableView源.但是,即使以为我看过其他文章,也没有人帮我(我可以看到,这是我第一个真正的iOS项目,因此我可能不是100%地处理他们的代码).我希望从此Plist生成表,并且由于它将至少有100个项目(通过Plist添加了更多项目),因此我希望生成该表.
但是,我主要要做的是获取数组以从Key1,Key2,Key3中获取信息,就像在下面的手动代码中那样(我必须手动输入,然后要添加更多信息,所以我希望它来自Plist):

I'm trying to get this code to converted to an array and used for a TableView source. However, even thought I've seen other posts, none help me out (That I can see, this is my first true iOS Project, so I might not be 100% at manipulating their code). I want the table to be generated from this Plist, and since it's going to have at least 100 items (With more being added via the Plist), I'd like for it to be generated.
However, the main thing I'm struggling to do is get the array to get the information from Key1, then Key2, Key3... like it would in the following manual code (Which I have to type out manually, and I'll be adding more info, so I want it to come from the Plist):

NSDictionary *item1 = [[NSDictionary alloc] initWithObjectsAndKeys:
                          @"Name1", @"Name", @"001", @"Number", nil]; 
NSDictionary *item2 = [[NSDictionary alloc] initWithObjectsAndKeys:
                              @"Name2", @"Name", @"002", @"Number", nil];
NSDictionary *item3 = [[NSDictionary alloc] initWithObjectsAndKeys:
                              @"Name3", @"Name", @"003", @"Number", nil];

我想做的就是能够更改表格在数字和字母之间的显示顺序,我认为这没有太大关系,但是我想把它放在那里,如果我在某种意义上犯了一个错误,那是无法做到的?
谢谢您的阅读,
约瑟夫·达菲

One thing I would like is to be able to change the order the table is shown in (Between number and alphabetic), which I don't think is hugely relevant, but it's something I'd like to throw in there, incase I'm making a mistake with something meaning that can't be done?
Thank you for reading,
Joseph Duffy

已解决,但有新问题,请阅读以下内容. 现在解决了,谢谢! 谢谢您提供的信息,它非常有用!但是,当我输入以下内容时:

Solved, but new issue, please read below. Now solved, thank you! Thank you for the information, it has been most useful! However, when I put the following:

- (void)viewDidLoad
{
    NSBundle *bundleLocation = [NSBundle mainBundle];
    NSString *arrayLocation = [bundleLocation pathForResource:@"plistname" ofType:@"plist"];
    NSArray *plistcontentsArray = [[NSArray alloc] initWithContentsOfFile:arrayLocation];
    self.pokemonNames = pokemonArray;

    [bundleLocation release];
    [plistArray release];

    [super viewDidLoad];
}

一切正常.但是,当我添加[arrayLocation release];时,它会因"exc_bad_access"而崩溃.有任何想法吗? :)

Everything works fine. But when I add [arrayLocation release]; it crashes with "exc_bad_access". Any ideas? :)

推荐答案

在属性列表中仅使用"array"元素而不是"dict"元素.例如:

Simply use the "array" element instead of the "dict" element in your property list. For example:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array>
    <dict>
        <key>Name</key>
        <string>Name1</string>
        <key>Number</key>
        <string>001</string>
    </dict>
    <dict>
        <key>Name</key>
        <string>Name2</string>
        <key>Number</key>
        <string>002</string>
    </dict>
</array>
</plist>

您还只是要存储名称,还是要在嵌套词典中存储更多信息?如果没有,则可以简化为:

Also are you just going to store names or are you going to store more information in the nested dictionaries? If not then this can be reduced down to:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array>
    <string>Name1</string>
    <string>Name2</string>
</array>
</plist>

要将数组加载到属性列表中,只需使用以下内容. (将"Array"更改为属性列表的名称,并且更有可能使用NSArray的实例变量.)

To load the array in the property list simply use the following. (Changing "Array" to the name of the property list and more than likely using an instance variable for the NSArray.)

NSBundle * mainBundle = [NSBundle mainBundle];
NSString * arrayLocation = [mainBundle pathForResource:@"Array" ofType:@"plist"];
NSArray * array = [[NSArray alloc] initWithContentsOfFile:arrayLocation];

并将信息从数组中拉出,您可以使用:

and to pull the information out of the array you can use:

NSString * name = [array objectAtIndex:0];

关于按字母顺序排列,您可以在这里找到如何进行:

As for doing alphabetical order you can find how to do that here:

http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Collections/Articles/Arrays.html#//apple_ref/doc/uid/20000132-SW5

这篇关于从Plist读取信息以在TableView中使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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