0

我正在解析一个 json 文件,然后尝试将一个数组传递给 cellForRowAtIndexPath 但该数组变为空。在.h

@interface NewsControllor : UITableViewController
@property (nonatomic) NSArray *latestNews;
@property (nonatomic) NSArray *sortedArray;
@end

@synthesize latestNews;
@synthesize sortedArray;

会出现视图

-(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

4

1 回答 1

0

首先尝试记录描述查找数据:

NSLog(@"NSData description: %@", [data description]);

如果它记录了一些有效的十六进制数据,那么您正在从指定的 URL 接收数据。
否则,您的NSURLConection.

于 2012-05-08T05:52:54.863 回答