2012-10-17 83 views
0

我想用数据填充gridView。我正在使用从Web服务返回的数据执行此操作。为了填补我有一个属性loopIndex计数阵列的哪个对象应该是下一个单元格。你可以在这里看到CellForRowAtIndexPath的方法。GridView总是在向下滚动时向下滚动

- (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"]; 

它不会崩溃。有谁知道我可以如何解决它?

亲切的问候

+0

您是否尝试在其上放置一个中断点并逐句通过代码以查看错误的位置? – Dcritelli

回答

0

尝试

_loopIndex++; 
    if(_loopIndex >= [[self getTeam]count]){ 
     _loopIndex = [[self getTeam]count]-1; 
    } 

不要做正确,你应该做如下:

cell = // new default cell; 

// calculate _loopindex for next attempt 
if (_loopindex < [[self getTeam]count]) 
{ 
    //do your job 
} 

return cell; 

如果设置正确行数在部分和表,你应该有方法gridView cellForItemAtIndexPath根据需要调用确切次数。不多也不少。这会阻止您返回零或计算错误_loopindex计数器。

+0

谢谢你的回答,但它仍然与相同的错误崩溃。 –

+0

正确计算循环索引检查表格是否合适。检查网格视图是否被调用了所需的次数。 –

+0

这对我有用。谢谢! –

0

你错误计算_loopindex这就是为什么你会得到一个错误,但更重要的是你应该依靠indexPath变量而不是你计算的索引来显示你的数据在这个协议方法。