我正在为当地的足球俱乐部构建一个应用程序。我想在网格中显示所有玩家的姓名和图片。因此我使用的是 NRGridview。但它不会加载我的数据。我有一个包含所有玩家信息的 NSArray。在这里,您可以看到生成此数组的方法。
- (NSArray *)getTeam
{
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Team"];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"sortOrder" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[request setSortDescriptors:sortDescriptors];
NSError *error = nil;
NSArray *mutableFetchResults = [self.genkDatabase.managedObjectContext executeFetchRequest:request error:&error];
NSLog(@"first error log %@", [error localizedDescription]);
if (mutableFetchResults == nil) {
NSLog(@"second error log %@", [error localizedDescription]);
}else if ([mutableFetchResults count] == 0){
NSLog(@"geen resultaten voor team");
}else{
NSLog(@"team names: %@",[mutableFetchResults valueForKey:@"name"]);
return mutableFetchResults;
}
return mutableFetchResults;
}
这就是我在表格视图中所做的。
- (NRGridViewCell*)gridView:(NRGridView *)gridView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *MyCellIdentifier = @"MyCellIdentifier";
NRGridViewCell* cell = [gridView dequeueReusableCellWithIdentifier:MyCellIdentifier];
if(cell == nil){
cell = [[NRGridViewCell alloc] initWithReuseIdentifier:MyCellIdentifier];
[[cell textLabel] setFont:[UIFont boldSystemFontOfSize:11.]];
[[cell detailedTextLabel] setFont:[UIFont systemFontOfSize:11.]];
}
NSLog(@"players array %@",players);
for (int i = 0; i <= [players count]; i++) {
// NSData *imgData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:[[players objectAtIndex:i]valueForKey:@"image"]]];
// UIImage *image = [[UIImage alloc]initWithData:imgData];
//cell.imageView.image = image;
cell.textLabel.text = [[players objectAtIndex:i]valueForKey:@"name"];
cell.detailedTextLabel.text = [[players objectAtIndex:i]valueForKey:@"position"];
return cell;
}
return cell;
}
NSLog 总是给出(null)。我现在的问题是,我应该把代码放在哪里“ NSArray *players = [self getTeam]
。这样我的表格视图就会填满数据?
编辑
它确实给了我正确数量的部分和 numberOfRowsInsection。对于 numbersOfRowsIn 部分,我创建了 4 个方法。1 种方法让守门员全部离开,后卫 1 次,边锋 1 次,攻击者 1 次。然后在我的 tableview 方法中,我做了以下事情。
- (NSInteger)gridView:(NRGridView *)gridView numberOfItemsInSection:(NSInteger)section
{
if(section == 0){
return [[self getDoelmannen]count];
}else if (section == 1){
return [[self getVerdedigers]count];
}else if (section == 2){
return [[self getMiddenvelders]count];
}else{
return [[self getAanvallers]count];
}
return [[self getAanvallers]count];
}
这行得通。但是我的手机仍然有问题。
EDIT2 好的,我认为我的问题在于填充我的玩家数组。我在 viewDidLoad 中执行以下操作
-(void)viewDidLoad{
_players = [self getTeam];
NSLog(@"players array: %@",_players);
}
这给出了以下日志。
2012-10-17 12:11:22.099 RacingGenk[63122:c07] nil
2012-10-17 12:11:22.099 RacingGenk[63122:c07] players array: (null)
这是我的 getTeam 代码
- (NSArray *)getTeam
{
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Team"];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"sortOrder" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[request setSortDescriptors:sortDescriptors];
NSError *error = nil;
NSArray *mutableFetchResults = [self.genkDatabase.managedObjectContext executeFetchRequest:request error:&error];
if (mutableFetchResults == nil) {
NSLog(@"nil");
}else if ([mutableFetchResults count] == 0){
NSLog(@"geen resultaten voor team");
}else{
NSLog(@"team names: %@",[mutableFetchResults valueForKey:@"name"]);
return mutableFetchResults;
}
return mutableFetchResults;
}