Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios JSON部分字符串_Ios_Json_Uitableview - Fatal编程技术网

Ios JSON部分字符串

Ios JSON部分字符串,ios,json,uitableview,Ios,Json,Uitableview,如何在Xcode中返回单元格中字符串的一部分 我有一个json响应: {"Email":"john@appleseed.com","Password":"4321","lijst":"eieren, kaas, melk, 2 uien, brood(volkoren)"} 如何获取xcode中不同单元格中“lijst”处的值? 因此,单元格1=Eieren 细胞2=Kaas 等等 这是在一个单元格中显示所有内容的代码: - (UITableViewCell *)tableView:(UITa

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

我有一个json响应:

{"Email":"john@appleseed.com","Password":"4321","lijst":"eieren, kaas, melk, 2 uien, brood(volkoren)"}
如何获取xcode中不同单元格中“lijst”处的值? 因此,单元格1=Eieren 细胞2=Kaas 等等

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

- (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;
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{
静态NSString*CellIdentifier=@“Cell”;
UITableViewCell*单元格=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
如果(!单元格)
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1重用标识符:CellIdentifier];
开关(indexPath.row){
案例0:
cell.textlab.text=[self name];
cell.detailTextLabel.text=@“Name”;
打破
案例1:{
NSString*email=[详细信息objectForKey:@“email”];
如果(!电子邮件)
电子邮件=@“无电子邮件”;
如果([email iskindof class:[NSArray class]])
电邮=@;
cell.textlab.text=电子邮件;
cell.detailTextLabel.text=@“电子邮件”;
打破
}
案例2:
cell.textlab.text=self.details[@“lijst”];
cell.detailTextLabel.text=@“列表”;
打破
违约:
打破
}
返回单元;

}

您似乎已将此JSON解析到名为
self.details
的字典中。然后,正如@bdesham所指出的,您可以获得一个包含
组件的单个值的数组,这些组件由字符串分隔:
。例如,如果您有一个名为
lijstArray
NSArray
属性,则可以执行以下操作:

NSString *lijstString = self.details[@"lijst"];
self.lijstArray  = [lijstString componentsSeparatedByString:@", "];
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]]];
}
然后,
lixstaray[0]
将有第一个值,
lixstaray[1]
第二个值,以此类推。值的数量是
[lixstaray count]

只有当您知道逗号后只有一个空格时,上述方法才有效。如果您不确定这一点,只需使用“
@”和“
作为分隔符,然后修剪空白。例如,如果将
lijstaray
属性声明为
NSMutableArray
,则可以执行以下操作:

NSString *lijstString = self.details[@"lijst"];
self.lijstArray  = [lijstString componentsSeparatedByString:@", "];
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]]];
}
无论采用哪种方法,在填充完数组后,
lijstaray
,现在都可以在
cellforrowatinexpath
中使用该方法。例如,假设您的tableview有两个部分,第0部分是姓名和电子邮件,第1部分是个人
lizstarray
值,例如:

- (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;
}
-(NSInteger)节数表视图:(UITableView*)表视图
{
返回2;
}
-(NSInteger)表视图:(UITableView*)表视图行数节:(NSInteger)节
{
如果(节==0)
返回2;
else if(节==1)
返回[self.starray count];
return 0;//我们永远不应该到这里,但是本着防御编程的精神,让我们确保处理好这个问题
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{
静态NSString*CellIdentifier=@“Cell”;
UITableViewCell*单元格=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
如果(!单元格)
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1重用标识符:CellIdentifier];
if(indexPath.section==0){
开关(indexPath.row){
案例0:
cell.textlab.text=[self name];
cell.detailTextLabel.text=@“Name”;
打破
案例1:{
NSString*email=[详细信息objectForKey:@“email”];
如果(!电子邮件)
电子邮件=@“无电子邮件”;
如果([email iskindof class:[NSArray class]])
电邮=@;
cell.textlab.text=电子邮件;
cell.detailTextLabel.text=@“电子邮件”;
打破
}
违约:
打破
}
}else if(indexPath.section==1){
cell.textlab.text=self.lijstaray[indexPath.row];
}
返回单元;
}

您可以使用NSJSONSerialization将JSON字符串解析到NSDictionary中。然后使用将字符串拆分为NSString数组。至于“eieren,kaas,melk,…”,这是一个单字符字符串,您必须自己解析它,可能需要使用NSString函数之一。我不太熟悉objective-C,我知道在R中有一个函数,它将u在引号之间的值分开。C语言中有类似的东西吗?你能举个例子吗?请参考文档!Objective-C库提供了一些非常好的文档,您应该先学习参考这些文档,然后再来SO寻求建议。谢谢您的帮助。如果我想自动递增,我应该只做一个for循环,比如for(i=0;i<[lijstValues count];i++)Case i:…@brian不太可能,我不这么认为。如果要将这些单独的值显示为表中的单元格,则需要将这些值放入将设置为类属性的数组中,例如
lijstArray
,然后修改
UITableViewDataSource
方法,以获取适合表中相应行的单独项。请参阅修订后的答案,例如,如果希望在单个表视图单元格中显示这些内容,您可以执行哪些操作。我知道您要执行的操作,但是如果我创建了LijstValues not found的属性,例如NSArray*LijstValues,它可以工作,但随后陷入addObject:问我是否要使用_lijstValues。@brian很抱歉输入错误。我在代码示例中更改了该变量的名称,但并没有在任何地方修复它。根据需要调用数组属性,然后相应地使用它。请参阅修订后的答案,我在其中通篇使用了
lijstaray
。但想法很简单:创建数组属性,在解析来自服务器的结果时填充它,然后在
cellforrowatinexpath
中使用该数组。