我需要帮助我的代码。我从一个web服务得到经度和纬度作为响应。我需要将它们转换为位置名称,并在相应的表视图单元格标签中显示。
以下是我视图的响应、代码和屏幕截图,其中我显示了我转换后的城市、国家名称,并更正了我错误的代码。
#import <UIKit/UIKit.h>
#import "JobTableViewCell.h"
#import "SlideNavigationController.h"
#import <CoreLocation/CoreLocation.h>
@interface JobPostedViewController : UIViewController <SlideNavigationControllerDelegate, UITableViewDataSource, CLLocationManagerDelegate>
@property (strong, nonatomic) NSArray* job_id;
@property (strong, nonatomic) NSArray* assigned_user_id;
@property (strong, nonatomic) NSArray* job_title;
@property (strong, nonatomic) NSArray* job_description;
@property (strong, nonatomic) NSArray* job_priority;
@property (strong, nonatomic) NSString* job_longitude;
@property (strong, nonatomic) NSArray* date;
@property (strong, nonatomic) NSString* job_latitude;
@property (strong, nonatomic) NSArray* job_completed;
@property (strong, nonatomic) NSArray* job_start_date;
@property (strong, nonatomic) NSArray* bids;
@property (nonatomic, strong) CLGeocoder *myGeocoder;
- (IBAction)onClickJobButton:(id)sender;
@property (weak, nonatomic) IBOutlet UIButton *sideNavigation;
@property (weak, nonatomic) IBOutlet UITableView *jobPostedTableView;
@end
`
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.jobPostedTableView.dataSource = self;
//Slide Navigation
[self.sideNavigation addTarget:[SlideNavigationController sharedInstance] action:@selector(toggleLeftMenu) forControlEvents:UIControlEventTouchUpInside];
WebManager *manager = [WebManager sharedInstance];
[manager getJobPostedWithCompletionBlock:^(id response){
NSDictionary *dictionary = (NSDictionary *)response;
// Read data from JSON
NSDictionary *responseObject = [dictionary objectForKey:@"response"];
NSLog(@"The Array%@",responseObject);
self.bids = [responseObject objectForKey:@"bids"];
self.job_description = [responseObject objectForKey:@"job_description"];
self.job_id = [responseObject objectForKey:@"job_id"];
self.job_completed = [responseObject objectForKey:@"job_completed"];
self.job_latitude = [responseObject objectForKey:@"job_latitude"];
self.job_longitude = [responseObject objectForKey:@"job_longitude"];
self.job_priority = [responseObject objectForKey:@"job_priority"];
self.job_start_date = [responseObject objectForKey:@"job_start_date"];
self.job_title = [responseObject objectForKey:@"job_title"];
[self.jobPostedTableView reloadData];
}];
CLGeocoder *ceo = [[CLGeocoder alloc]init];
CLLocation *loc = [[CLLocation alloc]initWithLatitude:[self.job_latitude floatValue] longitude:[self.job_longitude floatValue]]; //insert your coordinates
[ceo reverseGeocodeLocation:loc
completionHandler:^(NSArray *placemarks, NSError *error) {
CLPlacemark *placemark = [placemarks objectAtIndex:0];
NSLog(@"placemark %@",placemark);
//String to hold address
[[placemark.addressDictionary valueForKey:@"FormattedAddressLines"] componentsJoinedByString:@", "];
NSLog(@"addressDictionary %@", placemark.addressDictionary);
NSArray *ar = [placemark.addressDictionary objectForKey:@"locality"];
NSLog(@"placemark %@",ar); // Extract the city name
NSLog(@"placemark %@",placemark.country); // Give Country Name
}
];
}
#pragma mark - SlideNavigationController Methods
- (BOOL)slideNavigationControllerShouldDisplayLeftMenu
{
return YES;
}
#pragma mark - TableView Data Source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.job_title count];
}
- (NSInteger) numberOfSectionsInTableview:(UITableView *)tableView
{
return 1;
}
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"JobTableViewCell";
JobTableViewCell *cell = (JobTableViewCell *)[self.jobPostedTableView dequeueReusableCellWithIdentifier:cellIdentifier];
if(cell == nil) {
cell = [[JobTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
// Comparing array string to display an urgent image
if ([[self.job_priority objectAtIndex:indexPath.row]
isEqualToString:@"urgent"]) {
cell.urgentLabel.hidden = NO;
} else {
cell.urgentLabel.hidden = YES;
}
// Condition whether job completed is open or closed
if ([[self.job_completed objectAtIndex:indexPath.row]
isEqualToString:@"1"]) {
cell.jobStatus.text = @"Open";
[cell.jobStatus setTextColor:[UIColor colorWithRed:(84/255.f) green:(56/255.f) blue:(255/255.f) alpha:1.0f]];
cell.flagImage.image = [UIImage imageNamed:@"jobPosted_opened.PNG"];
} else {
cell.jobStatus.text = @"Closed";
[cell.jobStatus setTextColor:[UIColor colorWithRed:(179/255.f) green:(179/255.f) blue:(180/255.f) alpha:1.0f]];
cell.flagImage.image = [UIImage imageNamed:@"jobPosted_closed.PNG"];
}
cell.jobTitle.text = [self.job_title objectAtIndex:indexPath.row];
cell.jobContent.text = [self.job_description objectAtIndex:indexPath.row];
cell.bidsLabel.text = [[self.bids objectAtIndex:indexPath.row ]stringValue];
return cell;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}