【问题标题】:how to display json pattern in tableview如何在tableview中显示json模式
【发布时间】:2016-05-03 10:08:33
【问题描述】:

我是 IOS 新手,我需要将 json 获取的数据显示到 uitableview 我的 json 模式如下所示。

{"output":[{"id":{"php":"1"},"name":{"php":"ramkumar"},"image":{"1":"http:\/\/www.easyteach.gr\/users\/tutor\/1432636961.jpg"}},{"id":{"php":"2"},"name":{"php":"rakshi"},"image":{"2":"http:\/\/www.easyteach.gr\/users\/tutor\/1440371842.jpg"}},{"id":{"android":"1"},"name":{"android":"Vijayan"},"image":{"3":"http:\/\/www.easyteach.gr\/users\/tutor\/1432636961.jpg"}},{"id":{"android":"2"},"name":{"android":"vishali"},"image":{"4":"http:\/\/www.easyteach.gr\/users\/tutor\/1440371842.jpg"}},{"id":{"android":"3"},"name":{"android":"Niranjan"},"image":{"5":"http:\/\/www.easyteach.gr\/users\/tutor\/1432636961.jpg"}}]}

viewdidload 编码:

我在 viewdidload 本身中将 json url 创建为数组格式,并在 nslog 中成功显示,但在 tableview 中可能会失败。

if(data){
        NSError *error;
        NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options: kNilOptions error:&error];


        NSLog(@"%@",json);
        nameArray = [json valueForKeyPath:@"output.name"];
        php = [nameArray valueForKey:@"php"];


    }
    [_tabledata reloadData];

Tableview 代表:

-(NSInteger)numberOfSectionsInTableView:(UITableView *) tableView{

    return 1;
}

-(NSInteger)tableView:(UITableView *) tableView numberOfRowsInSection:(NSInteger)section{

    return [php count];

}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    TableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"table" forIndexPath:indexPath];

    cell.iteam.text=[php objectAtIndex:indexPath.row];

    return cell;
}

-(void)tableView: (UITableView *) tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{

}

【问题讨论】:

  • 控制台是否有任何错误?
  • 我认为这是因为您的 nameArray var 是 nil 因为您的 json 中没有任何路径是 output.name 并且您的 php var 也将为零。
  • 是的,我遇到了类似 thread@sandeeptomar 的错误
  • 它在 nslog name 数组中的显示值来了,php 也来了@tx2
  • NSLog php 你会明白的

标签: ios json uitableview


【解决方案1】:

这是一个有点奇怪的问题,但这里有一些提示。

首先,您没有提到 php 变量是什么,但我认为它是 NSArray。在将其发送到UITableView 之前,可能值得检查一下您期望显示多少元素:

php = [nameArray valueForKey:@"php"];
NSLog(@"There are %d items to be displayed", (int)php.count);

接下来,几年前我遇到了一个问题,我的UITableView 没有触发我的各种 UITableView 委托函数。

Have a read of this article 看看我需要做什么,并尝试在您的委托函数上添加一些断点以查看它们是否正在被调用。

最后,您的cellForRowAtIndexPath 函数似乎每次都会创建一个新的TableViewCell 项目:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    TableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"table" forIndexPath:indexPath];

    cell.iteam.text=[php objectAtIndex:indexPath.row];

    return cell;
}

如果操作系统不能重用现有对象,应该有一些代码只创建一个新单元:

TableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"table" forIndexPath:indexPath];
if (cell == nil) {
    cell = TableViewCell *cell=[[CompanyTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"table"];
}

我也很想插入一行代码,设置默认 textLabel,看看有没有出现:

cell.textLabel.font = [php objectAtIndex:indexPath.row];

希望这能有所帮助...

更新

实际上,我认为您误解了 JSON 字符串。

您的 JSON 包含一个元素 output,它是一组记录。

所以(我没有尝试过)我认为你应该像这样定义php

    NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options: kNilOptions error:&error];
    php = [json valueForKey:@"output"];

应该为您提供UITableView 使用的数组。但从那里,您需要访问每条记录的相关元素。

{
    "id": {
            "php": "1"
    },
    "name": {
        "php": "ramkumar"
    },
    "image": {
        "1": "http:\/\/www.easyteach.gr\/users\/tutor\/1432636961.jpg"
    }
}

目前,您的cellForRowAtIndexPath 函数会将text 设置为整个 记录:

cell.iteam.text=[php objectAtIndex:indexPath.row];

【讨论】:

    【解决方案2】:

    使用下面的代码行。希望对你有帮助:

    if(data){
        NSError *error;
        NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options: kNilOptions error:&error];
    
    
        NSLog(@"%@",json);
        nameArray = [json valueForKeyPath:@"output.name"];
        php = [[nameArray valueForKey:@"php"] mutableCopy];
     [php removeObjectIdenticalTo:[NSNull null]];
    
    
    }
    [_tabledata reloadData];
    

    【讨论】:

      猜你喜欢
      • 2022-01-15
      • 2020-03-28
      • 1970-01-01
      • 1970-01-01
      • 2021-07-13
      • 2021-11-05
      • 1970-01-01
      • 2019-01-25
      • 1970-01-01
      相关资源
      最近更新 更多