Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.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
Xcode cellForRowAtIndexPath中的数组变为空_Xcode_Arrays_Json_Ios5 - Fatal编程技术网

Xcode cellForRowAtIndexPath中的数组变为空

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

我正在解析一个json文件,然后试图将数组传递给CellForRowatineXpath,但数组变为null。 in.h

英寸

视图将出现

-(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]);