我正在解析一个 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