如何为 UIpicker 行分配 ID 和值?

Posted

技术标签:

【中文标题】如何为 UIpicker 行分配 ID 和值?【英文标题】:how to assign ID and value for UIpicker rows? 【发布时间】:2013-02-19 20:43:09 【问题描述】:

这是我使用 JSON 填充 UIPicker 的场景。

这是我的 JSON

"id":["232","432"],"contact":["Administrator","Main"]

我将数据存储到一个 NSdictionary 中,然后在转换为对象后,我创建了一个 NSarray 来存储联系人。

我可以使用 NSJSONserialization 将 contact 值显示到下拉列表中。

我想在我的 JSON 帖子请求中发送所选联系人对应的 id

【问题讨论】:

【参考方案1】:

您错误地填充了数据源。你应该有这样的模型:

联系人:[ id:“12”,姓名:“管理员”, id:“13”,姓名:“Dude”]

将其解析为数组后,在-pickerView:titleForRow:forComponent: 中,您必须从中选择一个 NSDictionary 值。通过-pickerView:didSelectRow:inComponent: 方法从从数据源获取的字典中获取 id 值。

编辑

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component

  NSNumber *idNumber = _contacts[row][@"id"];
  //perform whatever action you need to with selected contact id

【讨论】:

谢谢我已经调整了我的 JSON ,但我不明白你所说的抓取 Grab the id value from the dictionary fetched from your data source 是什么意思,请详细说明一下谢谢。 做了一个简短的编辑,你现在应该能明白我的意思了【参考方案2】:

尝试使用这样的标签将 id 设置为 UIPicker,

picker.tag = 232;

【讨论】:

以上是关于如何为 UIpicker 行分配 ID 和值?的主要内容,如果未能解决你的问题,请参考以下文章