Reputation: 83
I've created an application using rest kit 0.20.3 and xcode 5. I've created 2 objects on my server. But I'm unable to display the data in the tableview. Below is the code for my table view: PlayerViewController.m
#import "PlayerViewController.h"
#import <RestKit/RestKit.h>
#import "Player.h"
@interface PlayerViewController ()
@end
@implementation PlayerViewController
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
UIRefreshControl *refreshControl = [UIRefreshControl new];
[refreshControl addTarget:self action:@selector(loadPlayers) forControlEvents:UIControlEventValueChanged];
self.refreshControl = refreshControl;
[self loadPlayers];
[self.refreshControl beginRefreshing];
}
-(void)loadPlayers{
[[RKObjectManager sharedManager] getObjectsAtPath:@"/players.json" parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
[self.tableView reloadData];
[self.refreshControl endRefreshing];
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
[self.refreshControl endRefreshing];
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"An Error Has Occurred" message:[error localizedDescription] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alertView show];
}];}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return players.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"PlayerCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
Player *player = [players objectAtIndex:indexPath.row];
cell.textLabel.text = player.playerName;
return cell;
}
the players array count shows 0. I know i'm missing something here, but can't seem to figure it out. Thank You.
Upvotes: 1
Views: 500
Reputation: 15566
You need to assign the data to your players array:
[[RKObjectManager sharedManager] getObjectsAtPath:@"/players.json"
parameters:nil
success:^(RKObjectRequestOperation *operation,
RKMappingResult *mappingResult) {
self.players = mappingResult.array; // <-- doing the actual assignment
[self.tableView reloadData];
[self.refreshControl endRefreshing];
} // ... the rest of your code
This assumes you have have your mappings correct.
Upvotes: 3