Ios 未在UITableView中加载JSON数据

Ios 未在UITableView中加载JSON数据,ios,objective-c,json,uitableview,Ios,Objective C,Json,Uitableview,我有一个函数可以ping ebay并将JSON返回到iOS应用程序。我要做的是用返回的JSON填充UITableView。我已经对代码进行了设置,以我对如何工作的理解为准,但是表加载时没有任何数据。我认为这可能是因为表是在返回JSON之前加载的,但不确定如何修复 MatchCenterViewController.h #import <UIKit/UIKit.h> #import <Parse/Parse.h> #import "AsyncImageView.h" #im

我有一个函数可以ping ebay并将JSON返回到iOS应用程序。我要做的是用返回的JSON填充UITableView。我已经对代码进行了设置,以我对如何工作的理解为准,但是表加载时没有任何数据。我认为这可能是因为表是在返回JSON之前加载的,但不确定如何修复

MatchCenterViewController.h

#import <UIKit/UIKit.h>
#import <Parse/Parse.h>
#import "AsyncImageView.h"
#import "SearchViewController.h"

@interface MatchCenterViewController : UIViewController <UITableViewDataSource>

@property (nonatomic) IBOutlet NSString *itemSearch;
@property (nonatomic, strong) NSArray *imageURLs;

@property (strong, nonatomic) NSString *matchingCategoryCondition;
@property (strong, nonatomic) NSString *matchingCategoryLocation;
@property (strong, nonatomic) NSNumber *matchingCategoryMaxPrice;
@property (strong, nonatomic) NSNumber *matchingCategoryMinPrice;


@property (strong, nonatomic) NSArray *matchCenterArray;


@end

加载数据后,在主线程上重新加载表

在完成块中:

[PFCloud callFunctionInBackground:@"MatchCenterTest"
                   withParameters:@{
                                    @"test": @"Hi",
                                    }
                            block:^(NSDictionary *result, NSError *error) {

                                if (!error) {
                                    dispatch_async(dispatch_get_main_queue(), ^{
                                        self.matchCenterArray = [result objectForKey:@"Top 3"];
                                        [matchCenter reloadData];
                                    });
                                    NSLog(@"Test Result: '%@'", result);
                                }
                            }];

Edit1:另外,我建议您在一个地方适应您的协议。现在您正在头文件中使用
UITableViewDataSource
,然后在实现文件中同时使用
UITableViewDataSource
UITableViewDelegate
。坚持在一个位置进行最佳实践


Edit2:根据@Rob在评论中的建议,重新定位行,将
matchCenterArray
内容设置到主线程调度上


Edit3:根据注释,您可以更改JSON。上面列出的当前结构相当麻烦,因此我建议使用更简洁(更容易解析)的结构,如下所示:

    {
    "Top 3" : [
        {
            "Title" : "Apple iPhone 5s (Latest Model) - 16GB - Silver (AT&T) Smartphone",
            "Price" : "400.0",
            "Image URL" : "http://thumbs2.ebaystatic.com/m/mewfVG0QbBiu1nZytMuAlZw/140.jpg",
            "Item URL" : "http://www.ebay.com/itm/Apple-iPhone-5s-Latest-Model-16GB-Silver-AT-T-Smartphone-/181431570117?pt:Cell_Phones"
        },
        {
            "Title" : "Apple iPhone 5c (Latest Model) - 16GB - Pink (Verizon) Smartphone",
            "Price" : "350.0",
            "Image URL" : "http://thumbs4.ebaystatic.com/m/mMPAT67KjfCZF9oorbTf3uw/140.jpg",
            "Item URL" : "http://www.ebay.com/itm/Apple-iPhone-5c-Latest-Model-16GB-Pink-Verizon-Smartphone-/191204844039?pt:Cell_Phones"
        },
        {
            "Title" : "Apple iPhone 5 16GB, White, works with Virgin Mobile US NEW",
            "Price" : "359.99",
            "Image URL" : "http://thumbs3.ebaystatic.com/m/m5x1uj1iSS2fr691tifrvrw/140.jpg",
            "Item URL" : "http://www.ebay.com/itm/Apple-iPhone-5-16GB-White-works-Virgin-Mobile-US-NEW-/141227441998?pt:Cell_Phones"
        }
    ]
}
现在,您可以像这样访问您的值:

// In your completion block:
self.matchCenterArray = [result objectForKey:@"Top 3"];

...
[[self.matchCenterArray objectAtIndex:0] objectForKey:@"Title"] // title of the first object
[[self.matchCenterArray objectAtIndex:2] objectForKey:@"Image URL"] // image URL of the last item

从云中接收数据时,只需在块中重新加载表视图

- (void)viewDidLoad
{

[super viewDidLoad];

self.matchCenterArray = [[NSArray alloc] init];
//perform search with criteria just submitted
[PFCloud callFunctionInBackground:@"MatchCenterTest"
                   withParameters:@{
                                    @"test": @"Hi",
                                    }
                            block:^(NSDictionary *result, NSError *error) {




                                if (!error) {
                                    self.matchCenterArray = [result objectForKey:@"Top 3"];
[yourTableView reloadData];
                                    NSLog(@"Test Result: '%@'", result);
                                }
                            }];


}

在.h文件中还包括UITableViewDelagete。并在块之前初始化数组。

这对于web服务调用很重要,但由于表正在重新加载,因此它不是必需的。我认为。完成块在后台线程上执行。更新表是一项UI操作,因此需要在主线程上完成。这会导致一个错误,指出在MatchCenterViewController类型的对象上未找到
属性tableview。这是我应该添加到标题中的属性吗?如果是,它是如何格式化的?啊-您的tableView是在不同的变量名下设置的。检查编辑的代码。+1顺便说一句,您可能希望将分派块中的
self.matchCenterArray
分配放入主队列,否则您可能会在主队列忙于使用该数组响应UI事件(如滚动事件)时,处于后台更改的竞态状态。您通常希望同步主线程使用的模型对象的更新,而简单地将这些更新发送到主队列是最简单的方法。Hmm。这个解决方案是有道理的,但似乎仍然没有普及。如果我错了,请纠正我,但我认为这是因为我有一个视图控制器,其中包含一个表,而该表没有正确地子类化。我在问题中添加了两个屏幕截图以澄清问题。@Ghobs您正在
[[result objectForKey:@“Top 3”]objectAtIndex:0]objectForKey:@“Title”]中查找
标题
当它真正位于
[result objectForKey:@“Top 3”]objectAtIndex:0]objectForKey:@“Item 1”]objectAtIndex:0]objectForKey:@“Title”]
对于“Item 2”(表中的第二个单元格)和“Item 3”来说,这两个字典的键不同,因为它们的键不同。你是说你有能力改变JSON的结构吗?因为如果是这样的话,我可以就如何以不同的格式提出建议。
// In your completion block:
self.matchCenterArray = [result objectForKey:@"Top 3"];

...
[[self.matchCenterArray objectAtIndex:0] objectForKey:@"Title"] // title of the first object
[[self.matchCenterArray objectAtIndex:2] objectForKey:@"Image URL"] // image URL of the last item
- (void)viewDidLoad
{

[super viewDidLoad];

self.matchCenterArray = [[NSArray alloc] init];
//perform search with criteria just submitted
[PFCloud callFunctionInBackground:@"MatchCenterTest"
                   withParameters:@{
                                    @"test": @"Hi",
                                    }
                            block:^(NSDictionary *result, NSError *error) {




                                if (!error) {
                                    self.matchCenterArray = [result objectForKey:@"Top 3"];
[yourTableView reloadData];
                                    NSLog(@"Test Result: '%@'", result);
                                }
                            }];


}