gpt4 book ai didi

TableView Controller 中的 iOS/AFNetworking TableView 不显示从响应对象填充的数组中的任何数据

转载 作者:行者123 更新时间:2023-11-29 10:43:09 25 4
gpt4 key购买 nike

我对 iOS 开发还很陌生,我遇到了以下问题:

我正在使用 AFNetworking 从 RestFul WebService 检索数据,虽然它工作正常并且调试显示数据已成功检索并存储在字典数组中(它是 JSON 输出),但我的 TableView 根本没有更新数据。

下面是我的代码,用于我自己的方法和 viewDidLoad。

- (NSArray *) fetchLocations
{
// set up AFNetworking, connect and fetch the locations.
AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:[NSURL URLWithString:self.serviceURL]];
// set up object serializer and content types accepted as a response
manager.responseSerializer = [AFJSONResponseSerializer serializer];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"application/json"];
manager.responseSerializer.acceptableContentTypes = [manager.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"];

NSDictionary *parameters = @{@"client_uid": @"3232", @"session_key": self.sessionKey};

// initialize a new array then add objects to it.
NSMutableArray *locationsList = [[NSMutableArray alloc] init];

[manager POST:@"" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
// Process Response Object
NSDictionary *response = (NSDictionary *) responseObject;
// get the result, if it's null no point in going on just display an Alert View
NSNumber *resultState = [responseObject objectForKey:@"result"];
if ([resultState integerValue] == 0) {
// something happened, display an error message
}
else if([resultState integerValue] == 1) {
// we got an OK response, process the Dictionary data and return the array of objects.
NSDictionary *locationsOutput = [response objectForKey:@"output"];

for (NSString* key in locationsOutput) {
[locationsList addObject:[locationsOutput objectForKey:key]];
NSLog(@"Value is: %@", [locationsOutput objectForKey:key]);

}
NSLog(@"Locations added %i ", [locationsList count]);
}

} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
// Handle Error
UIAlertView *httpErrorView = [[UIAlertView alloc] initWithTitle:@"Request error" message:@"A service request error occurred. Please try again later" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[httpErrorView show];
}];
return locationsList;
}

// and viewDidLoad
- (void)viewDidLoad
{
[super viewDidLoad];

// 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;
if([self.parentViewController isKindOfClass:[BTMainViewController class]])
{
BTMainViewController *parent = (BTMainViewController *)self.parentViewController;
self.sessionKey = parent.session;
NSLog(@"URL is %@ ", self.sessionKey);
}

// also set the URL
self.serviceURL = [apiURL stringByAppendingString:@"/myrestfulurlhere"];
// fetch locations
self.locations = [[self fetchLocations] mutableCopy];
NSLog(@"Locations found %i ", [locations count]);
//self.tableView.delegate = self;
//self.tableView.dataSource = self;
//[self.tableView reloadData];
}

如有任何帮助,我们将不胜感激。我赞扬 dataSource 和 delegate 的设置,因为在之前的项目中根本不需要它们,而我的 UITableViewController 似乎可以正常工作。

UITableViewDataSource 和 UITableViewDelegate 方法已经符合协议(protocol)。

谢谢!

最佳答案

您调用的 AFNetworking 方法是异步的,这意味着它在返回实际结果之前返回。你必须取消评论

   self.tableView.delegate = self;
self.tableView.dataSource = self;

然后打电话
[self.tableView reloadData];
从网络操作的完成 block 开始:
NSLog(@"Locations added %i ", [locationsList count]);

关于TableView Controller 中的 iOS/AFNetworking TableView 不显示从响应对象填充的数组中的任何数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23472797/

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