gpt4 book ai didi

ios - raywenderlich 的 AFNetworking 教程不在表格单元格上显示数据

转载 作者:行者123 更新时间:2023-11-28 19:35:21 24 4
gpt4 key购买 nike

我正在学习 AFNetworking,

https://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;
}

我的 TableView Controller 中有这段代码。

此方法在上面代码中的委托(delegate)方法中使用:

- (NSDictionary *)currentCondition
{
NSDictionary *dict = self[@"data"];
NSArray *ar = dict[@"current_condition"];
return ar[0];
}

- (NSArray *)upcomingWeather
{
NSDictionary *dict = self[@"data"];
return dict[@"weather"];
}

我收到 json 格式的响应,但单元格为空。如有遗漏,请引用教程。

谢谢。

最佳答案

数据接收成功,你只是打印数据。您必须将该数据存储在字典中并重新加载 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];

}];

关于ios - raywenderlich 的 AFNetworking 教程不在表格单元格上显示数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39528814/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com