我仍然对iOS开发很新,我遇到了以下问题:
我正在使用AFNetworking从RestFul WebService检索数据,虽然它正常工作并且调试显示数据已成功检索并存储在Dictonaries数组(它的JSON输出)中,但我的表视图不会更新完全没有数据。
我的代码如下,对于我自己的方法和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方法已经符合协议。
谢谢!
答案 0 :(得分:2)
您调用的AFNetworking方法是异步,这意味着它会在返回实际结果之前返回。你必须取消注释
self.tableView.delegate = self;
self.tableView.dataSource = self;
并致电
在此之后从网络运营的完成块中[self.tableView reloadData];
:
NSLog(@"Locations added %i ", [locationsList count]);