我想用数据填充gridView。我正在使用从 Web 服务返回的数据来执行此操作。为了填满,我有一个属性 loopIndex ,它计算数组的哪个对象应该是下一个单元格。您可以在此处查看 CellForRowAtIndexPath 方法。
- (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(@"loopIndex: %d",_loopIndex);
_players = [self getTeam];
NSData *imgData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:[[_players objectAtIndex:_loopIndex]valueForKey:@"image"]]];
UIImage *image = [[UIImage alloc]initWithData:imgData];
cell.imageView.image = image;
cell.textLabel.text = [[_players objectAtIndex:_loopIndex]valueForKey:@"name"];
cell.detailedTextLabel.text = [[_players objectAtIndex:_loopIndex]valueForKey:@"position"];
if(_loopIndex < [[self getTeam]count]){
_loopIndex++;
}else {
_loopIndex = _loopIndex;
}
return cell;
}
在这里你会看到错误。它总是在 NSData 线上崩溃。
*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[_PFArray objectAtIndex:]: index (30) beyond bounds (30)'
我认为我的循环索引有问题。因为当我只使用静态值时。让我们说 1O 像这样
cell.textLabel.text = [[_players objectAtIndex:_loopIndex]valueForKey:@"name"];
它不会崩溃。有人知道我该如何解决吗?
亲切的问候