【问题标题】:How to use json dictionary, so that is the key is blank then value will not select?如何使用 json 字典,所以 key 为空然后 value 不会选择?
【发布时间】:2016-03-30 19:18:38
【问题描述】:

我有 NSDictionary 元素和我在桌子上显示的这本字典,如果从这些 dict 中选择任何元素,我会做什么,这将显示在文本字段上。

dict :{
    "" = "Select Elements";
    1 = lengha;
    2 = Choli;
    3 = Pyjami;
    4 = Dupatta;
    5 = Salwar;
    6 = Yoke;
    8 = Body;
}

现在我必须这样做,如果键为空,则不会在文本字段上选择值。即“选择元素”这将不会在文本字段上选择,或者如果它被选中,则数据将不会保存并显示一些保存信息。

  UITableViewCell *selectedCell = [itemTable cellForRowAtIndexPath:indexPath];
        NSString *elemCell = selectedCell.textLabel.text;
        NSMutableArray *arrayElements = Nil;
        if([elementsField.text isEqualToString:@""])
        {
            arrayElements = [NSMutableArray new];

        }
        else
        {
            arrayElements = [[elementsField.text componentsSeparatedByString:@","]mutableCopy];

        }
        if ([arrayElements containsObject: elemCell])
        {
            [arrayElements removeObject:elemCell];
        }
        else
        {
            [arrayElements addObject: elemCell];

        }
        NSString *str = [arrayElements componentsJoinedByString:@","];

        elementsField.text = @"";
        elementsField.text = str;
        [itemTable deselectRowAtIndexPath:indexPath animated:YES];
        itemTable.hidden = YES;

    }

【问题讨论】:

  • 不清楚你的实际问题是什么,但我可以告诉你,当表格视图滚动和单元格被重用时,根据单元格的内容做出决定会给你带来问题;您应该根据数据模型的内容做出决定。
  • 我希望“选择元素”不会被选中
  • 一种方法是在didSelectRowAtIndexPath 中,如果它是第一行/空行,您只需取消选择该行

标签: ios objective-c json uitableview dictionary


【解决方案1】:

该 JSON 数据定义错误,应如下所示:

choices : {
    "title" = "Select Elements",
    "elements" =  [
        "lengha",
        "Choli",
        "Pyjami",
        "Dupatta",
        "Salwar",
        "Yoke",
        "Body"
    ]
}

这将解决您的问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-04
    • 2015-04-04
    • 2012-09-07
    • 2020-02-12
    • 1970-01-01
    • 2014-06-07
    相关资源
    最近更新 更多