Xcode cellForRowAtIndexPath中的数组变为空
我正在解析一个json文件,然后试图将数组传递给CellForRowatineXpath,但数组变为null。 in.h 英寸 视图将出现Xcode cellForRowAtIndexPath中的数组变为空,xcode,arrays,json,ios5,Xcode,Arrays,Json,Ios5,我正在解析一个json文件,然后试图将数组传递给CellForRowatineXpath,但数组变为null。 in.h 英寸 视图将出现 -(void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; dispatch_async(kBgQueue, ^{ NSData* data = [NSData dataWithContentsOfURL: NewsFeed
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
dispatch_async(kBgQueue, ^{
NSData* data = [NSData dataWithContentsOfURL:
NewsFeed];
[self performSelectorOnMainThread:@selector(fetchedData:)
withObject:data waitUntilDone:YES];
});
}
获取的数据
- (void)fetchedData:(NSData *)responseData {
//parse out the json data
NSError* error;
NSDictionary* json = [NSJSONSerialization
JSONObjectWithData:responseData //1
options:kNilOptions
error:&error];
self.latestNews = [json objectForKey:@"News"]; //2
//sort the json array
NSSortDescriptor *latestnewssorter = [[NSSortDescriptor alloc] initWithKey:@"sortNumber" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:latestnewssorter,nil];
self.sortedArray =[latestNews sortedArrayUsingDescriptors:sortDescriptors];
}
cellForRowAtIndexPath
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
{
NSLog(@"News: %@", self.latestNews);
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:@"NewsCell"];
NSDictionary* item = [self.sortedArray objectAtIndex:0]; //[indexPath row]
cell.textLabel.text = [item objectForKey:@"titleOfNews"];
cell.detailTextLabel.text = [item objectForKey:@"detailOfNews"];
return cell;
}
我正在测试,这就是为什么我只返回1个单元格。我在节中的行数也是1首先尝试记录描述查找数据:
NSLog(@"NSData description: %@", [data description]);
如果它记录了一些有效的十六进制数据,那么您正在从指定的URL接收数据。否则,您的
nsurlconnection
有问题,我在(fetchedData)中进行了日志输出,并打印了一个数组。但是如果我将日志打印输出放在cellForRowAtIndexPath中,数组返回空值。因此,我可以获取数据源并对其进行排序,但它在CellForRowatineXpath中变为null。我将retain放在属性中,但仍然会变为null。我确实发现它和线程有关,但我认为nsarray是线程安全的。当我返回到主线程并打印日志时,数组为空
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
{
NSLog(@"News: %@", self.latestNews);
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:@"NewsCell"];
NSDictionary* item = [self.sortedArray objectAtIndex:0]; //[indexPath row]
cell.textLabel.text = [item objectForKey:@"titleOfNews"];
cell.detailTextLabel.text = [item objectForKey:@"detailOfNews"];
return cell;
}
NSLog(@"NSData description: %@", [data description]);