将 PList 中的数据获取到 UITableView 中?

发布于 2024-12-23 01:43:04 字数 1247 浏览 1 评论 0原文

我想维护一个记录列表,对于每个记录,我维护相同类型的数据。我想在两个不同的地方使用这些数据:

  1. UITableView 从每个记录中获取“名称”值
  2. UIViewController 获取要在不同字段中使用的所有数据。

我认为我应该使用 plist 来存储数据;我还假设应该接收 UITableView 数据的对象是 NSArray,因此我可以使用 cellForRowAtIndexPath 方法自动创建表。

所以我创建了plist“PLForArr.plist”: 在此处输入图像描述

看来我只能在使用调用 plist 时获得 NSDictionary

NSString *path = [[NSBundle mainBundle] pathForResource:@"PLForArr" ofType:@"plist"];
NSArray * myArr = [[NSArray alloc] initWithContentsOfFile:path]; //doesn't work...
NSDictionary * myDict = [[NSDictionary alloc] initWithContentsOfFile:path]; //does work, but who to I make a NSArray out of it / or get the data from here to the UITableView?

我明白我不明白这里有一些基本的东西...有人可以一步步指导我:

  1. 如何保留数据 - 我应该使用 plist 还是其他东西?我是否应该有一个数组类型的主要记录(就像我在此处的示例 plist 中所做的那样),或者我可以将其保留为这些字典,而无需考虑 UITableViewMyArr 我使用的不必要的 MyArr代码>结束目标?
  2. 如何调用数据 - 有没有办法将其放入NSArray或者必须放入NSDictionary
  3. 如何将其调用到 UITableView - 我可以使用 NSDictionary 填充行吗?

I want to maintain a list of records, for each one I maintain the same type of data. I want to use this data in 2 different places:

  1. UITableView that takes from each record the "Name" value
  2. UIViewController that takes all the data to use in different fields.

I assume I should be using a plist to store the data; I also assume that the object that should be receiving the data for the UITableView is NSArray so I can use the cellForRowAtIndexPath method to create the table automatically.

So i created the plist "PLForArr.plist":
enter image description here

It seems that i can only get a NSDictionary when calling the plist using

NSString *path = [[NSBundle mainBundle] pathForResource:@"PLForArr" ofType:@"plist"];
NSArray * myArr = [[NSArray alloc] initWithContentsOfFile:path]; //doesn't work...
NSDictionary * myDict = [[NSDictionary alloc] initWithContentsOfFile:path]; //does work, but who to I make a NSArray out of it / or get the data from here to the UITableView?

I understand that i don't understand something basic here... Can someone take me step by step on:

  1. How to keep the data - should I use plist or something else? Should I have a main record of type array (as I did on the example plist here) or can I just keep it as these Dictionaries without the unnecessary MyArr that I used considering the UITableView end target?
  2. How to call the data - Is there a way to get it into a NSArray or must it get into a NSDictionary?
  3. How to call it into the the UITableView - Can I fill in the lines using a NSDictionary?

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

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

发布评论

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

评论(2

触ぅ动初心 2024-12-30 01:43:04
  1. 将数据存储为数组或字典取决于您。但是,如果您想随着时间的推移对其进行更改,则无法将其存储在主包中。

  2. 您的 pList 文件是包含数组的字典。请参阅下面的代码示例。

  3. 您必须将字典存储在数组中作为表的数据源。请参阅下面的代码示例。

假设你的UITableView的数据源叫做tableArray。您可以使用 tableArray 来填充表和视图中的信息。哦,是的,

NSString *path = [[NSBundle mainBundle] pathForResource:@"PLForArr" ofType:@"plist"];
NSDictionary *myDict = [[NSDictionary alloc] initWithContentsOfFile:path];

NSArray *myArray = [myDict objectForKey:@"MyArray"];
self.tableArray = [myArray copy];
[myArray release];
[myDict release];

这在 tableView 中:cellForRowAtIndexPath:

cell.text = [[tableArray objectAtIndex:row]objectForKey:@"Obj Name"];
  1. Storing the data is an Array or a Dictionary is up to you. But if you want to make changes to it over time you can't store it in the main bundle.

  2. Your pList file is a dictionary that contains an array. See code example below.

  3. You will have to store the dictionary in an array for the data source for your table. See code example below.

Assuming that your UITableView's data source is called tableArray. You can use tableArray to fill in the information in the table and your view. Oh yeah,

NSString *path = [[NSBundle mainBundle] pathForResource:@"PLForArr" ofType:@"plist"];
NSDictionary *myDict = [[NSDictionary alloc] initWithContentsOfFile:path];

NSArray *myArray = [myDict objectForKey:@"MyArray"];
self.tableArray = [myArray copy];
[myArray release];
[myDict release];

This goes in tableView: cellForRowAtIndexPath:

cell.text = [[tableArray objectAtIndex:row]objectForKey:@"Obj Name"];
稚然 2024-12-30 01:43:04
  1. 将数据存储在字典中还是数组中取决于您。根据您拥有的数据类型,您将考虑存储无序的对象集合(字典),使用访问条目;或者更确切地说,在有序集合(数组)中,使用数字索引

  2. 从属性列表文件中获取数组很好,但是根(顶级)对象是一个字典(在屏幕截图中,“MyArr”不是顶级对象,它是访问数组的关键顶级词典)。要从中获取数组,只需按照您的方式分配/初始化 plist 字典,然后使用其键 ([myDict objectForKey:@"MyArr"]) 访问数组条目。否则,请确保将属性列表的根对象设置为数组,然后重试 NSArrayinitWithContentsOfFile:

  3. 真正的问题似乎是 我怎样才能用我的数据填充单元格?表视图询问其委托和数据源要显示多少个部分、一个部分中的行。根据这些数字,它将向数据源询问单元格。同样,根据您选择的存储类型,您将以稍微不同的方式实现这些方法,但概念仍然存在。

您可能需要阅读有关以下内容的文档:

  1. Storing your data either in a dictionary, or in an array is up to you. Depending on the kind of data you have, you will consider storing unordered collection of objects (dictionary), accessing the entries with keys; or rather in ordered collection (array), using numeric indexes.

  2. It's fine to get arrays from property list files, but the root (top level) object is a dictionary (in the screenshot, "MyArr" isn't the top-level object, it is the key for accessing your array in the top-level dictionary). To get your array from it, simply alloc/init the plist dictionary the way you did, and access the array entry using its key ([myDict objectForKey:@"MyArr"]). Otherwise make sure you set the root object of the property list to be an array, and retry NSArray's initWithContentsOfFile:

  3. The real question seems to be How can I fill the cells with my data ? The table views ask its delegate and dataSource about how many sections, rows in a section, to display. Based on these numbers, it will ask the dataSource for cells. Once again depending on the storage type you've chosen, you will implements these methods a little bit differently, but the concepts remain.

You will probably want to read documentation about :

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