【问题标题】:JSON partial stringJSON 部分字符串
【发布时间】:2014-04-18 18:52:42
【问题描述】:

如何在 Xcode 的单元格中返回字符串的一部分?

我有一个 json 响应:

{"Email":"john@appleseed.com","Password":"4321","lijst":"eieren, kaas, melk, 2 uien, brood(volkoren)"}

如何在 xcode 的不同单元格中获取“lijst”的值? 所以单元格 1 = Eieren 单元格 2 = 卡斯 等等……

这是在一个单元格中显示所有内容的代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell)
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];

switch (indexPath.row) {
    case 0:
        cell.textLabel.text = [self name];
        cell.detailTextLabel.text = @"Name";
        break;
    case 1: {
        NSString *email = [details objectForKey:@"Email"];
        if (!email)
            email = @"No email";
        if ([email isKindOfClass:[NSArray class]])
            email = @"<Multiple emails>";
        cell.textLabel.text = email;
        cell.detailTextLabel.text = @"Email";
        break;
    }
    case 2:
        cell.textLabel.text = self.details[@"lijst"];
        cell.detailTextLabel.text = @"List";
        break;
    default:
        break;
}

return cell;

}

【问题讨论】:

  • 您将使用 NSJSONSerialization 将 JSON 字符串解析为 NSDictionary。
  • 然后使用-[NSString componentsSeparatedByString:]将字符串拆分成NSStrings的NSArray。
  • 至于“eieren, kaas, melk,...”,这是一个单一的字符串,你必须自己解析它,可能使用 NSString 函数之一。
  • 我对 Objective-C 不是很熟悉,我知道在 R 中有一个函数可以将你放在引号之间的内容分开。 C中有类似的东西吗?可以举个例子吗
  • 请参考文档! Objective-C 库有一些非常好的文档,您应该学习首先参考它,然后再向 SO 寻求建议。

标签: ios json uitableview


【解决方案1】:

您似乎已将此 JSON 解析为名为 self.details 的字典。然后,正如@bdesham 指出的那样,您可以使用componentsSeparatedByString: 获取单个值的数组。例如,如果您有一个名为 lijstArrayNSArray 属性,您可以执行以下操作:

NSString *lijstString = self.details[@"lijst"];
self.lijstArray  = [lijstString componentsSeparatedByString:@", "];

然后,lijstArray[0] 将具有第一个值,lijstArray[1] 将具有第二个值,依此类推。值的数量是[lijstArray count]

仅当您知道逗号后只有一个空格时,上述内容才有效。如果您不确定,您只需使用@"," 作为分隔符,然后修剪空格。例如,如果您将lijstArray 属性声明为NSMutableArray,您可以执行以下操作:

NSString *lijstString = self.details[@"lijst"];
NSArray  *tempArray   = [lijstString componentsSeparatedByString:@","];

self.lijstArray = [NSMutableArray array];
for (NSString *lijstValue in tempArray) {
    [self.lijstArray addObject:[lijstValue stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]];
}

无论您采用哪种方法,在填充数组 lijstArray 后,您现在都可以在您的 cellForRowAtIndexPath 中使用它。例如,假设您的 tableview 有两个部分,第 0 部分是名称和电子邮件,第 1 部分是单独的 lijstArray 值,例如:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0)
        return 2;
    else if (section == 1)
        return [self.lijstArray count];

    return 0;  // we never should get here, but in the spirit of defensive programming, let's make sure we handle this
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (!cell)
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];

    if (indexPath.section == 0) {
        switch (indexPath.row) {
            case 0:
                cell.textLabel.text = [self name];
                cell.detailTextLabel.text = @"Name";
                break;
            case 1: {
                NSString *email = [details objectForKey:@"Email"];
                if (!email)
                    email = @"No email";
                if ([email isKindOfClass:[NSArray class]])
                    email = @"<Multiple emails>";
                cell.textLabel.text = email;
                cell.detailTextLabel.text = @"Email";
                break;
            }
            default:
                break;
        }
    } else if (indexPath.section == 1) {
        cell.textLabel.text = self.lijstArray[indexPath.row];
    }

    return cell;
}

【讨论】:

  • 感谢您的成功。如果我想自动递增,我应该只做一个 for 循环,比如 for(i = 0; i
  • @brian 不完全是,我不认为。如果要将这些单独的值显示为表格中的单元格,则需要将这些值放在一个数组中,该数组将设置为类属性,例如lijstArray,然后修改您的 UITableViewDataSource 方法以获取适合表中相应行的各个项目。例如,如果您想在单独的表格视图单元格中显示这些内容,请参阅修订后的答案。
  • 我知道你要去哪里,但是这给了我 LijstValues not found 的错误,如果我创建它的属性,比如 NSArray *lijstValues,它可以工作,但随后卡在 addObject 中:询问如果我想使用 _lijstValues。
  • @brian 抱歉打错了。我在我的代码示例中更改了该变量名称,并没有在任何地方修复它。随心所欲地调用您的数组属性,然后相应地使用它。请参阅修订后的答案,我在其中始终使用lijstArray。但想法很简单:创建数组属性,在解析服务器结果时填充它,然后在 cellForRowAtIndexPath 中使用该数组。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-08-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-27
  • 1970-01-01
相关资源
最近更新 更多