我可以用数字引用 NSDictionary 项目吗?

发布于 2024-09-01 06:02:35 字数 465 浏览 4 评论 0原文

我正在尝试设置一个充当表单的 UITableView。每个单元格内都有一个 UILabel 和一个 UITextField,因此一个单元格是:

Name          <enter name>
^             ^
UILabel       UITextField

现在,我尝试从 NSDictionary (来自 plist)填充 UILabel 和 UITextField,它的组织方式如下

Root            Type           Value
  Name          String
  Address       String
  City          String

  etc

: cellForRowAtIndexPath 方法中的右侧标签和文本字段,我必须以数字方式引用字典。有什么办法可以做到这一点吗?

I'm trying to set up a UITableView which acts as a form. Each cell has within it a UILabel and a UITextField, so one cell is:

Name          <enter name>
^             ^
UILabel       UITextField

Now, I'm trying to populate the UILabel and the UITextField from a NSDictionary (from a plist), where it's organized like so:

Root            Type           Value
  Name          String
  Address       String
  City          String

  etc

But in order to get the right labels and textfields in the cellForRowAtIndexPath method, I would have to refer to the Dictionary numerically. Is there any way to do that?

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

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

发布评论

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

评论(3

笑梦风尘 2024-09-08 06:02:35

不,因为 NSDictionary 中的元素没有排序。

您确实应该使用数组作为表视图数据源背后的模型。您可以轻松地为您的情况构建一个:

NSArray* myArray = [myDict allKeys];

当然,对于您的情况,需要特殊订单。所以你宁愿使用文字构建数组:

NSArray* myArray = [NSArray arrayWithObjects:@"Name", @"Address", @"City", nil];

No, since elements in an NSDictionary are not ordered.

You should really use an array as the model behind a table view data source. You can easily build one for your case:

NSArray* myArray = [myDict allKeys];

Of course, for your case, a special order would be neccessary. So you would rather build your array using literals:

NSArray* myArray = [NSArray arrayWithObjects:@"Name", @"Address", @"City", nil];
请持续率性 2024-09-08 06:02:35

严格来说,不。

我倾向于做的是创建一个单独的 NSArray 键,这些键是有序的,以便事物按正确的顺序出现。例如:

NSDictionary *values = ...
NSArray *keys = ....

for (NSString *key in keys) {
    id value = [values objectForKey:key];
    ...
}

正如其他人所指出的,有必要有一个有序列表(即 NSArray)来匹配 UITableView 的工作方式。然而,这不一定是数据本身。当数据是 NSDictionary 或 NSManagedObject 时,有一个常见的习惯用法是在表单中编辑该数据,这在 iPhoneOS 上几乎总是意味着 UITableView

在这种情况下,您很可能不想显示编辑对象中的所有元素,您将希望显示字段的标题,并且希望控制显示顺序。所有这些都意味着这是该情况的有效(未混淆)模式。

在这种特殊情况下,可以从 plist 中获取键,但以通用方式提取它们虽然可以通过解析 plist XML 来实现,但并非易事。

Strictly speaking, no.

What I tend to do is create a separate NSArray of keys, which are ordered so that things come out in the right sequence. eg:

NSDictionary *values = ...
NSArray *keys = ....

for (NSString *key in keys) {
    id value = [values objectForKey:key];
    ...
}

As others have pointed out, it is necessary to have an ordered list (ie, an NSArray) to match the way that a UITableView works. However, this doesn't have to be the data itself. When the data is a NSDictionary, or an NSManagedObject, there is a common idiom of editing that data in a form, which on iPhoneOS will almost always mean a UITableView.

In this case, you very probably won't want to show all the elements in the edited object, you will want to display titles for the fields, and will want to control the order of display. All of which means that this is a valid (not obfuscated) pattern for the situation.

In this particular case, the keys could be taken from the plist, but extracting them in a general way, although feasible by parsing the plist XML, is non-trivial.

相守太难 2024-09-08 06:02:35

您应该将您的 plist 重组为字典数组。字典键是您需要填充的元素,值是文本。按照您希望其在屏幕上显示的顺序将每个字典添加到 plist 数组中。这实际上就是“设置”应用程序的工作方式。保存用户输入的文本非常简单。

你的 plist 看起来像这样:

<root>
    <array>
        <dict>
            <key>labelText</key>
            <value>Name</value>
            <key>textFieldPromptText</key>
            <value>enter name</value>
            <key>textFieldText</key>
            <value>name entered by user</value>
        </dict> 
        <dict>
            <key>labelText</key>
            <value>Address</value>
            <key>textFieldPromptText</key>
            <value>enter street address</value>
            <key>textFieldText</key>
            <value>address entered by user</value>
        </dict>
        <dict>
            <key>labelText</key>
            <value>City</value>
            <key>textFieldPromptText</key>
            <value>enter city</value>
            <key>textFieldText</key>
            <value>city entered by user</value>
        </dict> 
        .
        .
        .
    </array>
</root>

You should restructure your plist to be an array of dictionaries. The dictionary keys are the element that you need to populate and the values the text. Add each dictionary to the plist array in the order that you want it to appear on screen. This is effectively how the Settings application works. Saving the text entered by the user is very straightforward.

Your plist will look something like:

<root>
    <array>
        <dict>
            <key>labelText</key>
            <value>Name</value>
            <key>textFieldPromptText</key>
            <value>enter name</value>
            <key>textFieldText</key>
            <value>name entered by user</value>
        </dict> 
        <dict>
            <key>labelText</key>
            <value>Address</value>
            <key>textFieldPromptText</key>
            <value>enter street address</value>
            <key>textFieldText</key>
            <value>address entered by user</value>
        </dict>
        <dict>
            <key>labelText</key>
            <value>City</value>
            <key>textFieldPromptText</key>
            <value>enter city</value>
            <key>textFieldText</key>
            <value>city entered by user</value>
        </dict> 
        .
        .
        .
    </array>
</root>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文