Search code examples
iosobjective-crestkit

Display RestKit feed in UITableView


Hi I am trying to display results pulled from open weather API in a tableview, my request shows the results but I haven't a clue how I could display this in my tableview.

- (void)viewDidLoad {
    [self getWeatherForCity:@"London"];
    [super viewDidLoad];
}

-(RKResponseDescriptor *)responseDescriptor {

    RKObjectMapping* cloudMapping = [RKObjectMapping mappingForClass:[Clouds class]];
    [cloudMapping addAttributeMappingsFromArray:@[@"all"]];
    RKObjectMapping* coordMapping = [RKObjectMapping mappingForClass:[Coord class]];
    [coordMapping addAttributeMappingsFromArray:@[@"lon",@"lat"]];
    RKObjectMapping* weatherReportMapping = [RKObjectMapping mappingForClass:[WeatherReport class]];
    [weatherReportMapping addAttributeMappingsFromArray:@[@"name"]];

    [weatherReportMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"clouds"
                                                                                         toKeyPath:@"clouds"
                                                                                       withMapping:cloudMapping]];

    [weatherReportMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"coord"
                                                                                         toKeyPath:@"coord"
                                                                                       withMapping:coordMapping]];

    RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:weatherReportMapping
                                                                                            method:RKRequestMethodAny
                                                                                       pathPattern:nil
                                                                                           keyPath:nil
                                                                                       statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
    return responseDescriptor;
}

-(void)getWeatherForCity:(NSString *)cityName{

    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://api.openweathermap.org/data/2.5/weather?q=%@",cityName]];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];

    RKObjectRequestOperation *objectRequestOperation = [[RKObjectRequestOperation alloc] initWithRequest:request
                                                                                     responseDescriptors:@[[self responseDescriptor]]];

    [objectRequestOperation setCompletionBlockWithSuccess:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
        //RKLogInfo(@"Load collection of Weather: %@", mappingResult.array);
        RKLogInfo(@"Load collection of Weather: %@", mappingResult.array);

    } failure:^(RKObjectRequestOperation *operation, NSError *error) {

        RKLogError(@"Operation failed with error: %@", error);
    }];

    [objectRequestOperation start];
}

Can someone please tell me how I would use the feed infer and return the amount of correct amount of rows in the tableview, and how I would set the cell.textlabel.text to something from the feed?


Solution

  • Here is a tutorial for using table view with an array - Link.

    You can use the following tutorial for reference of Restkit feed in UITableView - Link

    Suggestion: Please do a basic research on google before you ask a question here.