我正在尝试从JSON文件加载数据。我已将此文件添加到Xcode 4.6中的Project Files中,但每次运行我的代码时,我都会在TableView中得到空单元格,根本没有数据,即使我已经为每个标签声明了正确的文件并分配了一个来自JSON文件的值。
我创建了两个类:events.h
和.m
,jsonloader.h
和.m
,并将它们导入我的主视图控制器。
以下是我的JSON数据示例:
{
"Events": [
{
"id": "1",
"title": "parr jazz presents zoe chiotis",
"performingArtists": "Zoe Chiotis",
"day": "Tuesday",
"date": "2013-12-03",
"lastUpdated": "2013-12-03",
"startingTime": "8:00:00 PM",
"endingTime": "1:00:00 AM",
"venue": "STUDIO2",
"location": {
"streetAddress": "33-45 Parr Street",
"postCode": "L1 4JN"
},
"music genre": "Jazz",
"information": "ZOE CHIOTIS is a singer/songwriter/guitarist based in Manchester. ",
"image": "http://bandonthewall.org/images/content/large/zoe-chiotis-1764.jpg",
"thumb": "http://swingbands.co.uk/wp-content/zoechiotis.jpg"
},
...
]}
这是event.m
:
@implementation JSONLoader
- (NSArray *)eventsFromJSONFile:(NSURL *)url {
// Create a NSURLRequest with the given URL
NSURLRequest *request = [NSURLRequest requestWithURL:url
cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData
timeoutInterval:30.0];
// Get the data
NSURLResponse *response;
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];
// Now create a NSDictionary from the JSON data
NSDictionary *jsonDictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
// Create a new array to hold the locations
NSMutableArray *events = [[NSMutableArray alloc] init];
// Get an array of dictionaries with the key "Events"
NSArray *array = [jsonDictionary objectForKey:@"Events"];
// Iterate through the array of dictionaries
for(NSDictionary *dict in array) {
// Create a new event object for each one and initialise it with information in the dictionary
Events *dataevents = [[Events alloc] initWithJSONDictionary:dict];
// Add the Location object to the array
[events addObject:dataevents];
}
// Return the array of Location objects
return events;
}
在我的mainViewController.m
中我加入了这个:
@implementation MV_HomeViewController {
NSArray *_events;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
// Create a new JSONLoader with a local file URL
JSONLoader *jsonLoader = [[JSONLoader alloc] init];
NSURL *url = [[NSBundle mainBundle] URLForResource:@"EventsData" withExtension:@"json"];
// Load the data on a background queue...
// As we are using a local file it's not really necessary, but if we were connecting to an online URL then we'd need it
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
_events = [jsonLoader eventsFromJSONFile:url];
// Now that we have the data, reload the table data on the main UI thread
[self.myTableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:YES];
});
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [_events count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"eventCell";
MV_CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier
forIndexPath:indexPath];
if (cell == nil) {
cell = [[MV_CustomCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
}
// Configure the cell...
//[[cell textLabel] setText:_eventType];
Events *event = [_events objectAtIndex:indexPath.row];
cell.TitleLabel.text = event.title;
cell.GenreLabel.text = event.musicGenre;
return cell;
}
有人知道这里的问题是什么吗?