RESTKit で GET リクエストを実行していますが、テーブルがないとオブジェクトをマップできません。テーブルがあればオブジェクトをマッピングできます。テーブル内のラベルを 1 つのオブジェクトに設定し、背景にあるラベルを別のオブジェクトに設定すると、両方のラベルに正しくマッピングされたオブジェクトが表示されます。ただし、テーブルを削除してラベルだけを背景に残すと、マッピングが機能せず、ラベルに何も表示されません。私はむしろテーブルを持たず、オブジェクトを 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