我使用 rest kit 0.20.3 和 xcode 5 创建了一个应用程序。我在我的服务器上创建了 2 个对象。但我无法在表格视图中显示数据。下面是我的表格视图的代码: 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;
}
玩家数组计数显示为 0。我知道我在这里遗漏了一些东西,但似乎无法弄清楚。谢谢你。