【问题标题】:How to get value of key in dictionary after UITableViewCell is clicked in iOS?在iOS中单击UITableViewCell后如何获取字典中的键值?
【发布时间】:2017-04-24 16:04:10
【问题描述】:

我正在尝试在 UIViewController 中设置 UITableView。对我来说效果很好。

首先,我做了什么:

1) 我从服务器收到这样的响应:

[{"email_id":"adas@faga.gs","id":66,"mobile_no":"1236547895","name":"asad","relation":"dsfs"},{"email_id":"raj@ghj.com","id":67,"mobile_no":"5632145412","name":"raj","relation":"xyz"},{"email_id":"surajsukale@gmail.com","id":68,"mobile_no":"8698819943","name":"suraj","relation":"self"}]

2) 成功在原型单元格的所有字典中显示4个值。

即:email_idmobile_nonamerelation

注意:这里我得到 5 个参数作为响应,但我只显示 4 个。

这是我为此所做的:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [menuItems count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"EmergencyContactCell";

    EmergencyContactCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    NSDictionary *content = [menuItems objectAtIndex:indexPath.row];

    cell.name.text = content[@"name"];
    cell.email.text = content[@"email_id"];
    cell.mobile.text =content[@"mobile_no"];
    cell.relation.text =content[@"relation"];

    return cell;
}

它对我来说非常有用。

现在,我想要什么?

当我单击任何单元格时,我想获取该字典中的特定 ID。 我应该在这里做什么:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"%ld",(long)indexPath.row);

// What should i do here is my question ??
    }

请任何人都可以解决我的问题。帮助将是可观的。

【问题讨论】:

    标签: ios objective-c uitableview dictionary


    【解决方案1】:

    请用这个

     - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
        {
           int id = [[[menuItems objectAtIndex:indexPath.row]valueForKey:@"id"]intValue];
        }
    

    【讨论】:

      【解决方案2】:

      我认为您正在寻找类似的东西

      NSDictionary *content = [menuItems objectAtIndex:indexPath.row];
      NSNumber *number = content["id"];
      

      在你需要一个普通的数据类型时,使用适当的属性,例如:

      int iNumber = number.intValue;
      

      【讨论】:

        【解决方案3】:

        将以下代码写入您的didSelectRowAtIndexPath 方法:

        NSDictionary *content = [menuItems objectAtIndex:indexPath.row];
        NSString *id = content[@"id"];
        

        您尚未在列表中显示id,但您可以访问它。因为它在你的字典里。

        【讨论】:

          【解决方案4】:
           - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
             NSDictionary *content = [menuItems objectAtIndex:indexPath.row];
              NSString *id = content[@"id"];
           }
          

          但我不明白为什么虽然你解析了cellForRowAtIndexPath中的值,但你仍然要求在didSelectRowAtIndexPath中进行相同的处理?

          【讨论】:

            【解决方案5】:

            你可以用同样的方式访问你的 menuItems:

            - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
            {
                NSDictionary *yourItem = [menuItems objectAtIndex:indexPath.row];
            }
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2021-01-24
              • 2011-12-22
              • 1970-01-01
              相关资源
              最近更新 更多