数组在cellForRowAtIndexPath中变为null

时间:2012-05-08 05:14:33

标签: xcode arrays json ios5

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

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

in .m

@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

1 个答案:

答案 0 :(得分:0)

首先尝试记录数据的描述:

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

如果它记录了一些有效的十六进制数据,那么您将从指定的URL接收数据 否则,您的NSURLConection会出现问题。

相关问题