我正在使用 RESTKit 执行 GET 请求,如果没有表格,我无法映射对象。如果有表格,我可以映射对象。如果我将表格内的标签设置为一个对象,并将背景上的标签设置为另一个对象,则两个标签都显示正确映射的对象。但是,如果我删除表格并仅在背景上保留标签,则映射不起作用并且标签不显示任何内容。我宁愿没有表格,只想将 JSON 响应中的对象映射到视图控制器中的标签。这是我的 GET 请求代码(顺便说一句,moreresultsVCSobj 用于我用于全局变量的单例):
@interface MoreResultsViewController ()
@property (strong, nonatomic) NSArray *moreInfo;
@end
@implementation MoreResultsViewController
@synthesize nameLabel, idLabel = _idLabel, addressLabel = _addressLabel, specialtyGroupsLabel = _specialtyGroupsLabel, infoTableView;
- (void)viewDidLoad
{
[super viewDidLoad];
moreresultsVCSobj = [SingletonObject singleObj];
//[DejalBezelActivityView activityViewForView:self.view];
infoTableView.delegate = self;
infoTableView.dataSource = self;
[self displayInfo];
[self getDoctorInfo];
}
- (void)getDoctorInfo
{
//NOTE NOTE NOTE: There is a weird error with this code where RESTKit only maps one (unknown) object. I don't know how to fix this problem. Currently this method just isn't called, meaning it won't run.
//RK Logging
RKLogConfigureByName("RestKit/Network", RKLogLevelTrace);
NSIndexSet *statusCodeSet = RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful);
NSString *urlString = [NSString stringWithFormat:@"https://testGET.com/stuff/testGET];
NSURL *url = [NSURL URLWithString:urlString];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
NSLog(@"Connection ==> %@", connection);
//HTTP Header
NSMutableURLRequest *networksRequest = [NSMutableURLRequest requestWithURL:url];
[networksRequest setValue:moreresultsVCSobj.userAuthToken forHTTPHeaderField:@"Auth-Token"];
//Object Mapping
RKObjectMapping *networkMapping = [RKObjectMapping mappingForClass:[Info class]];
[networkMapping addAttributeMappingsFromDictionary:@{@"name": @"name", @"id": @"strenuusID", @"location_specialties.address_lines": @"address_lines"}];
NSString *pathPatternString = [NSString stringWithFormat:@"/stuff/testGET"];
//Response Descriptor
RKResponseDescriptor *networksResponseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:networkMapping
method:RKRequestMethodGET
pathPattern:pathPatternString
keyPath:@""
statusCodes:statusCodeSet];
RKObjectRequestOperation *networksOperation = [[RKObjectRequestOperation alloc] initWithRequest:networksRequest
responseDescriptors:@[networksResponseDescriptor]];
[networksOperation setCompletionBlockWithSuccess:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult)
{
NSLog(@"Mapping Results ==> %@", mappingResult.array);
self.moreInfo = mappingResult.array;
[self.infoTableView reloadData];
//[DejalBezelActivityView removeView];
}
failure:^(RKObjectRequestOperation *nameOperation, NSError *nameError)
{
NSLog(@"ERROR: %@", nameError);
NSLog(@"Response: %@", nameOperation.HTTPRequestOperation.responseString);
//[DejalBezelActivityView removeView];
}];
[networksOperation start];
}
//Number of Sections in the table
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
//This method runs when a row in the table is selected
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//Deselects row
[infoTableView deselectRowAtIndexPath:indexPath animated:YES];
}
// Customizes the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.moreInfo.count;
}
// Customize the appearance of table view cells. Pretty standard method and can be mostly reused minus 'cell.textLabel.text = -self.doctorNames objectAtIndex: [indexPath row]];
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *MoreInfoCellIdentifier = @"moreInfoCell";
MoreInfoCell *cell = [self.infoTableView dequeueReusableCellWithIdentifier:MoreInfoCellIdentifier];
Info *info = [self.moreInfo objectAtIndex:indexPath.row];
[self configureCell:cell forInfo:info];
return cell;
}
//Chooses what is displayed in the cell
- (void)configureCell:(MoreInfoCell *)cell forInfo:(Info *)info
{
//Sets labels in cell
cell.testLabel.text = info.strenuusID;
nameLabel.text = info.name;
NSLog(@"Doctor Name new GET: %@", info.strenuusID);
NSLog(@"Address new GET: %@", info.address_lines);
printf("\n");
}
- (void)displayInfo
{
Info *info = [Info new];
nameLabel.text = info.name;
_idLabel.text = moreresultsVCSobj.doctorIDString;
_addressLabel.text = moreresultsVCSobj.addressString;
_specialtyGroupsLabel.text = moreresultsVCSobj.specialtyGroupsString;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end