我正在开发一个应用程序,该应用程序根据Web服务提供的信息创建表视图。我目前有两个表视图,一个用于“Regions”,另一个用于“Categories”,并且使用RestKit将JSON信息映射到适当的类。我有“Region”和“Category”类来保存每个项目的数据。如果它是第一个要查看的表,那么每个表似乎都能正常工作。但是,如果我查看一个表然后切换到另一个表,程序将遇到异常。如果我在查看另一个表之前查看一个表,似乎让我的Category和Region类混淆了。
当我首先进入类别视图,然后转到Regions视图时,我得到以下异常: “由于未捕获的异常'NSInvalidArgumentException'终止应用程序,原因:' - [Category regionName]:无法识别的选择器发送到实例0x938f1a0'”
当我进入区域视图,然后是类别视图时,我看到以下内容: “由于未捕获的异常'NSInvalidArgumentException'终止应用程序,原因:' - [Region categoryName]:无法识别的选择器发送到实例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;
}