我正在使用
学习AFNetworkinghttps://www.raywenderlich.com/59255/afnetworking-2-0-tutorial
但是,对于json部分,我没有在本教程中显示的单元格上显示数据。
我收到了json格式的响应,但之后没有在单元格中显示任何内容。
static NSString * const BaseURLString = @"http://www.raywenderlich.com/demos/weather_sample/";
@interface WTTableViewController ()
@property(strong) NSDictionary *weather;
@end
@implementation WTTableViewController
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationController.toolbarHidden = NO;
[self.tableView setDelegate:self];
self.tableView.dataSource=self;
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if([segue.identifier isEqualToString:@"WeatherDetailSegue"]){
UITableViewCell *cell = (UITableViewCell *)sender;
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
WeatherAnimationViewController *wac = (WeatherAnimationViewController *)segue.destinationViewController;
NSDictionary *w;
switch (indexPath.section) {
case 0: {
w = self.weather.currentCondition;
break;
}
case 1: {
w = [self.weather upcomingWeather][indexPath.row];
break;
}
default: {
break;
}
}
wac.weatherDictionary = w;
}
}
#pragma mark - Actions
- (IBAction)clear:(id)sender
{
self.title = @"";
self.weather = nil;
[self.tableView reloadData];
}
- (IBAction)jsonTapped:(id)sender
{
NSString *string = [NSString stringWithFormat:@"%@weather.php?format=json", BaseURLString];
NSURL *url = [NSURL URLWithString:string];
//NSURLRequest *request = [NSURLRequest requestWithURL:url];
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
[manager GET:string parameters:nil progress:nil success:^(NSURLSessionTask *task, id responseObject) {
NSLog(@"JSON: %@", responseObject);
} failure:^(NSURLSessionTask *operation, NSError *error) {
NSLog(@"Error: %@", error);
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error Retrieving Weather"
message:[error localizedDescription]
delegate:nil
cancelButtonTitle:@"Ok"
otherButtonTitles:nil];
[alertView show];
}];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if(!self.weather)
return 1;
switch (section) {
case 0: {
NSLog(@"return 1");
return 1;
}
case 1: {
NSArray *upcomingWeather = [self.weather upcomingWeather];
NSLog(@"return upcomingWeather");
return [upcomingWeather count];
}
default:
return 0;
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{NSLog(@"tableView");
static NSString *CellIdentifier = @"WeatherCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
NSDictionary *daysWeather = nil;
switch (indexPath.section) {
case 0: {
daysWeather = [self.weather currentCondition];
NSLog(@"currentCondition");
break;
}
case 1: {
NSArray *upcomingWeather = [self.weather upcomingWeather];
daysWeather = upcomingWeather[indexPath.row];
NSLog(@"upcomingWeather");
break;
}
default:
break;
}
cell.textLabel.text = [daysWeather weatherDescription];
NSLog(@"textLabel = %@", cell.textLabel.text);
// Configure the cell...
return cell;
}
我的表视图控制器中有这个代码。
此方法用于上述代码中的委托方法:
- (NSDictionary *)currentCondition
{
NSDictionary *dict = self[@"data"];
NSArray *ar = dict[@"current_condition"];
return ar[0];
}
- (NSArray *)upcomingWeather
{
NSDictionary *dict = self[@"data"];
return dict[@"weather"];
}
我在json甲酸盐中得到了反应,但细胞是空的。 如果此处缺少任何内容,请参阅教程。
谢谢。
答案 0 :(得分:1)
收到数据后,您只需打印数据。 你必须将这些数据存储在字典中并重新加载tableview。
使用以下代码:
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
[manager GET:string parameters:nil progress:nil
success:^(NSURLSessionTask *task, id responseObject) {
self.weather = (NSDictionary *)responseObject; //add this line
[self.tableView reloadData]; //add this line
} failure:^(NSURLSessionTask *operation, NSError *error) {
NSLog(@"Error: %@", error);
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error Retrieving Weather"
message:[error localizedDescription]
delegate:nil
cancelButtonTitle:@"Ok"
otherButtonTitles:nil];
[alertView show];
}];
答案 1 :(得分:1)
您缺少来自成功阻止的代码
self.weather = (NSDictionary *)responseObject;
self.title = @"JSON Retrieved";
[self.tableView reloadData];
答案 2 :(得分:1)
只需在加载到dict
之前检查天气tableview
内的数据是否存在,并且在完成从服务器获取数据后,只需调用reloadData
的{{1}}方法即可加载您的代码稍后收到的数据。