我有一个函数可以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
MatchCenterViewController.m:
#import "MatchCenterViewController.h"
#import <UIKit/UIKit.h>
@interface MatchCenterViewController () <UITableViewDataSource, UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *matchCenter;
@end
@implementation MatchCenterViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
//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"];
NSLog(@"Test Result: '%@'", result);
}
}];
self.matchCenterArray = [[NSArray alloc] init];
}
- (void)viewDidAppear:(BOOL)animated
{
[PFCloud callFunctionInBackground:@"MatchCenterTest"
withParameters:@{
@"test": @"Hi",
}
block:^(NSDictionary *result, NSError *error) {
if (!error) {
NSLog(@"Test Result: '%@'", result);
}
}];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.matchCenterArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
NSDictionary *matchCenterDictionary= [self.matchCenterArray objectAtIndex:indexPath.row];
cell.textLabel.text = [matchCenterDictionary objectForKey:@"Title"];
if([matchCenterDictionary objectForKey:@"Price"] != NULL)
{
cell.detailTextLabel.text = [NSString stringWithFormat:@"$%@",[matchCenterDictionary objectForKey:@"Price"]];
}
return cell;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
返回JSON:
{
"Top 3" = (
{
"Item 1" = (
{
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";
}
);
},
{
"Item 2" = (
{
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";
}
);
},
{
"Item 3" = (
{
Title = "Apple iPhone 5 \U2013 16GB, White, works with Virgin Mobile US \U2013 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";
}
);
}
);
}
答案 0 :(得分:2)
加载数据后,在主线程上重新加载表。
在完成块中:
[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:重新定位行,将matchCenterArray
内容设置为主要线程调度,每个@ Rob的建议在评论中
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
答案 1 :(得分:0)
当您从云端接收数据时,只需在块中重新加载表格视图。
- (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。并在块之前初始化数组。