我正在开发一个应用程序,该应用程序根据 Web 服务提供的信息创建表视图。我目前有两个表视图,一个用于“区域”,另一个用于“类别”,并且正在使用 RestKit 将 JSON 信息映射到适当的类。我有“区域”和“类别”类来保存每个项目的数据。如果它是要查看的第一个表,则每个表似乎都可以正常工作。但是,如果我查看一个表然后切换到另一个,程序会遇到异常。如果我在查看另一张表之前查看一张表,似乎将我的类别和区域类混淆了。
当我首先转到类别视图,然后转到区域视图时,我收到此异常:“由于未捕获的异常 'NSInvalidArgumentException' 导致应用程序终止,原因:'-[Category regionName]: unrecognized selector sent to instance 0x938f1a0'”
当我进入 Regions 视图,然后是 Categories 视图时,我看到以下内容:“由于未捕获的异常 'NSInvalidArgumentException' 导致应用程序终止,原因:'-[Region categoryName]: unrecognized selector sent to instance 0xce87240'”
这是相关的代码。我意识到这是很多代码,但我不知道我的问题出在哪里:
如果有人对出了什么问题有任何想法,我将不胜感激。谢谢!
分类查看:
- (void)viewDidLoad
{
[super viewDidLoad];
mCategoryId = [NSNumber numberWithInt:0];
NSLog(@"Categories");
RKURL *baseURL = [RKURL URLWithBaseURLString:@"My URL"];
RKObjectManager *objectManager = [RKObjectManager objectManagerWithBaseURL:baseURL];
objectManager.client.baseURL = baseURL;
RKObjectMapping *categoryMapping = [RKObjectMapping mappingForClass:[Category class]];
[categoryMapping mapKeyPathsToAttributes:@"categoryID", @"categoryID", @"parentID", @"parentID", @"categoryName", @"categoryName", @"childrenCount", @"childrenCount", @"parentCount", @"parentCount", @"catCount", @"catCount", nil];
[objectManager.mappingProvider setMapping:categoryMapping forKeyPath:@""];
[self sendRequest];
}
- (void) sendRequest {
RKObjectManager *objectManager = [RKObjectManager sharedManager];
NSError *error;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:[NSDictionary dictionaryWithObject:mCategoryId forKey:@"CategoryId"] options:0 error:&error];
[objectManager loadObjectsAtResourcePath:@"/api/Main/Process" usingBlock:^(RKObjectLoader *loader) {
loader.params = [RKRequestSerialization serializationWithData:jsonData MIMEType:RKMIMETypeJSON];
loader.method = RKRequestMethodPOST;
loader.serializationMIMEType = RKMIMETypeJSON;
NSDictionary *headers = [NSDictionary dictionaryWithObjectsAndKeys:@"application/json", @"Content-Type", @"Basic ClientIdentifier%3Afc_ios_client%2CEndpoint%3A%2FCategories%2FGetChildCategoriesById", @"Authorization", nil];
loader.additionalHTTPHeaders = headers;
loader.onDidLoadObjects = ^(NSArray *objects) {
NSLog(@"Objects Loaded");
categories = objects;
[self.tableView reloadData];
};
loader.onDidFailWithError = ^(NSError *error) {
NSLog(@"Error %@", [error localizedDescription]);
};
NSLog(@"%@",[NSString stringWithFormat:@"%@%@", loader.URL.baseURL, loader.URL.relativePath]);
loader = nil;
}];
objectManager = nil;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
Category *category = [[Category alloc] init];
category = nil;
category = [categories objectAtIndex:indexPath.row];
NSLog(@"Category Cell");
cell.textLabel.text = category.categoryName;
NSLog(@"Cell returned");
return cell;
}
地区视图:
- (void)viewDidLoad
{
[super viewDidLoad];
NSLog(@"Region Listings Loading");
mRegionId = [NSNumber numberWithInt:1];
RKURL *baseURL = [RKURL URLWithBaseURLString:@"My URL"];
RKObjectManager *objectManager = [RKObjectManager objectManagerWithBaseURL:baseURL];
objectManager.client.baseURL = baseURL;
RKObjectMapping *regionMapping = [RKObjectMapping mappingForClass:[Region class]];
[regionMapping mapKeyPathsToAttributes:@"regionHome", @"regionHome", @"regionID", @"regionID", @"regionName", @"regionName", @"parentCount", @"parentCount", @"parentID", @"parentID", @"childrenCount", @"childrenCount", @"parentName", @"parentName", nil];
[objectManager.mappingProvider setMapping:regionMapping forKeyPath:@""];
NSLog(@"Mapping done");
[self sendRequest];
}
- (void) sendRequest {
RKObjectManager *objectManager = [RKObjectManager sharedManager];
NSError *error;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:[NSDictionary dictionaryWithObject:mRegionId forKey:@"RegionId"] options:0 error:&error];
NSLog(@"JSON Data");
[objectManager loadObjectsAtResourcePath:@"/api/Main/Process" usingBlock:^(RKObjectLoader *loader) {
loader.params = [RKRequestSerialization serializationWithData:jsonData MIMEType:RKMIMETypeJSON];
loader.method = RKRequestMethodPOST;
loader.serializationMIMEType = RKMIMETypeJSON;
NSDictionary *headers = [NSDictionary dictionaryWithObjectsAndKeys:@"application/json", @"Content-Type", @"Basic ClientIdentifier%3Afc_ios_client%2CEndpoint%3A%2FRegions%2FGetChildRegionsByIdWithBaseRegion", @"Authorization", nil];
loader.additionalHTTPHeaders = headers;
NSLog(@"Headers");
loader.onDidLoadObjects = ^(NSArray *objects) {
NSLog(@"Objects Loaded");
regions = nil;
regions = objects;
[self.tableView reloadData];
};
loader.onDidFailWithError = ^(NSError *error) {
NSLog(@"Error %@", [error localizedDescription]);
};
NSLog(@"%@",[NSString stringWithFormat:@"%@%@", loader.URL.baseURL, loader.URL.relativePath]);
}];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
Region *region = [[Region alloc] init];
region = nil;
region = [regions objectAtIndex:indexPath.row];
NSLog(@"region");
cell.textLabel.text = region.regionName;
return cell;
}