【发布时间】: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