2

Webサービスによって提供される情報からテーブルビューを作成するアプリケーションに取り組んでいます。現在、「Regions」用と「Categories」用の2つのテーブルビューがあり、RestKitを使用してJSON情報を適切なクラスにマッピングしています。各アイテムのデータを保持するための「Region」クラスと「Category」クラスがあります。最初に表示されるテーブルであれば、各テーブルは正常に機能しているようです。ただし、一方のテーブルを表示してからもう一方のテーブルに切り替えると、プログラムで例外が発生します。一方のテーブルを表示してからもう一方のテーブルを表示すると、CategoryクラスとRegionクラスが混同されているようです。

最初にCategoriesのビューに移動し、次にRegionsビューに移動すると、次の例外が発生します:「キャッチされない例外'NSInvalidArgumentException'によるアプリの終了、理由:'-[Category regionName]:認識されないセレクターがインスタンス0x938f1a0に送信されました」

リージョンビュー、次にカテゴリビューに移動すると、次のように表示されます。「キャッチされなかった例外'NSInvalidArgumentException'によるアプリの終了、理由:'-[リージョン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;
}
4

0 に答える 0