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
Objective c UITableView的JSON数据仍然为空_Objective C_Json_Uitableview_Ios7 - Fatal编程技术网

Objective c UITableView的JSON数据仍然为空

Objective c UITableView的JSON数据仍然为空,objective-c,json,uitableview,ios7,Objective C,Json,Uitableview,Ios7,我已经从我的网站加载了JSON数据,并将其添加到UITableView中,但该表仍然为空。。应用程序没有崩溃,所以我真的不知道出了什么问题,也许你们中的某个人可以帮助我。我将给出下面的代码以及文件所有者的连接 提前谢谢 我的JSON数据文件: [ { name: "XYZ", details: "XYZ"}, { name: "XYZ", details: "XYZ"} ] ViewController.h: #import <UIKit/UIKit.h> @interface

我已经从我的网站加载了JSON数据,并将其添加到
UITableView
中,但该表仍然为空。。应用程序没有崩溃,所以我真的不知道出了什么问题,也许你们中的某个人可以帮助我。我将给出下面的代码以及文件所有者的连接

提前谢谢

我的JSON数据文件:

[
{ name: "XYZ", details: "XYZ"},
{ name: "XYZ", details: "XYZ"}
]
ViewController.h:

 #import <UIKit/UIKit.h>

@interface ViewController : UIViewController<UITableViewDelegate,UITableViewDataSource>
{
    IBOutlet UITableView *tableData;
}

@end
Main.storyboard:

原型单元的重用标识符为“Item”

数据源
已连接到ViewController
代理
已连接到ViewController
tableData
已连接到ViewController

这就是我所做的一切。。。
非常感谢:)

viewDidLoad

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    NSURL *url = [NSURL URLWithString:@"http://example.com/JSON/index.php"];
    NSData *jsonSource = [NSData dataWithContentsOfURL:url];

    //store the JSON into an array
    myObject = [[NSMutableArray alloc] init];
    myObject = [NSJSONSerialization JSONObjectWithData:jsonSource 
                                options:NSJSONReadingMutableContainers error:nil];
    [self.tableView reloadData];
}
NumberOfSectionsTableView

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
行数部分

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return myObject.count;
}
cellForRowAtIndexPath

- (UITableViewCell *)tableView:(UITableView *)tableView 
                                cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *CellIdentifier = @"Item";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell=[[UITableViewCell alloc]initWithStyle:
              UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }

    NSDictionary * obj = myObject[indexPath.row];
    cell.textLabel.text = obj[@"name"] ;
    cell.detailTextLabel.text= obj[@"details"] ;

    return cell;
}

您的json响应中缺少键名的引号

[
  {
    "name": "XYZ",
    "details": "XYZ"
  },
  {
    "name": "XYZ",
    "details": "XYZ"
  }
]
此外,为了改进,请直接使用
jsonObjects
(目前您正在阅读
jsonObjects
并制作
myObject
,这与
jsonObjects
本身没有什么不同)

而且。。。由于您使用的是原型单元格,因此您当前在
-cellforrowatinexpath:
中的操作方式是错误的。
即:当前,您有一个
if(cell==nil)
块,该块。。。是无用的(对于原型单元格),您最终只能看到
cell.textlab.text

正确的方法:
  • 转到情节提要
  • 选择原型单元格
    • (通过右侧的界面生成器)将其样式从
      custom
      更改为
      subtitle
  • 最后 例如:

    - (void)viewDidLoad
    {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
    
        NSURL *url = [NSURL URLWithString:@"http://example.com/JSON/index.php"];
        NSData *jsonSource = [NSData dataWithContentsOfURL:url];
    
        //declare NSArray *jsonObjects; in the .h
        jsonObjects = [NSJSONSerialization JSONObjectWithData:jsonSource 
                                                      options:NSJSONReadingMutableContainers 
                                                        error:nil];
    }
    


    仅供参考:主要是json无效。

    如果您的json格式正确,您的原始代码可能会正常工作,但原型单元肯定需要正确安装。

    在解析结束时,请尝试在tableView上重新加载数据。
    我添加了
    [tableView重新加载数据]
    UITableViewCell
    方法的末尾,什么都没有发生。为什么不直接使用
    jsonObjects
    而放弃
    myObject
    ,因为
    myObject
    在技术上与
    jsonObjects
    没有什么不同。加上。。。为什么要使用
    objectforkeydsubscript
    ,我建议改用
    objectForKey
    ?(不确定,但这些建议可能不会真正帮助您解决核心问题,但至少会有所改进)此外。。。
    jsonSource
    是否有有效内容?在
    -viewDidLoad
    的末尾设置一个断点,检查
    jsonSource
    myObject
    的外观是否与您期望的一样
    viewDidLoad
    myObject
    的末尾有0个对象。。。如果我是对的,那么一定是解析失败了?之前我还认为objective-c可能无法正确读取php文件,但我没有找到一个将其命名为商业API的解决方案。我测试了您的解决方案,表仍然是空的,但感谢您的ideaok刚刚恢复工作,我现在使用的是另一个json php脚本,它可以工作了!我还没有测试它是否直接与我的代码一起工作,但是你的代码正在工作。所以我必须拼出一句非常感谢的话:)你做得很好。@Criska很好,祝你的应用程序好运
    ;-)
    @meda:Criska的问题是一个无效的json,问题评论中已经指出了这一点。你所建议的只是一种更有效的做事方式,但不会解决他的问题,因为
    myObject
    会一直保持
    nil
    ,直到json被修复为止(…所有这些都在评论中指出)。我只是在发布答案之前等待确定OP的核心问题。
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
    
        NSURL *url = [NSURL URLWithString:@"http://example.com/JSON/index.php"];
        NSData *jsonSource = [NSData dataWithContentsOfURL:url];
    
        //declare NSArray *jsonObjects; in the .h
        jsonObjects = [NSJSONSerialization JSONObjectWithData:jsonSource 
                                                      options:NSJSONReadingMutableContainers 
                                                        error:nil];
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *CellIdentifier = @"Item";
    
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
        //useless when using a prototype cell
        //if (cell == nil) {
        //    cell=[[UITableViewCell alloc]initWithStyle: UITableViewCellStyleSubtitle 
        //                               reuseIdentifier:CellIdentifier];
        //}
    
        //fyi: old style
        //NSString *strName = [[jsonObjects objectAtIndex:indexPath.row] objectForKey:@"name"];
        //NSString *strDetails = [[jsonObjects objectAtIndex:indexPath.row] objectForKey:@"details"];
    
        //new style (but doesn't matter which one you adopt)
        NSString *strName = jsonObjects[indexPath.row][@"name"];
        NSString *strDetails = jsonObjects[indexPath.row][@"details"];
    
        [cell.textLabel setText:strName];
        [cell.detailTextLabel setText:strDetails];
    
        return cell;
    }