我使用rest kit 0.20.3和xcode 5创建了一个应用程序。我在服务器上创建了2个对象。但是我无法在tableview中显示数据。下面是我的表视图的代码: 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.我知道我在这里缺少一些东西,但似乎无法弄明白。 谢谢。
答案 0 :(得分:3)
您需要将数据分配给您的球员阵列:
[[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
这假设您的映射是正确的。