Objective c 向下滚动时,GridView始终会崩溃
我想用数据填充gridView。我使用从Web服务返回的数据执行此操作。对于填充,我有一个属性loopIndex,它计算数组的哪个对象应该是下一个单元格。您可以在这里看到CellForRowatineXpath方法Objective c 向下滚动时,GridView始终会崩溃,objective-c,ios,uitableview,core-data,gridview,Objective C,Ios,Uitableview,Core Data,Gridview,我想用数据填充gridView。我使用从Web服务返回的数据执行此操作。对于填充,我有一个属性loopIndex,它计算数组的哪个对象应该是下一个单元格。您可以在这里看到CellForRowatineXpath方法 - (NRGridViewCell*)gridView:(NRGridView *)gridView cellForItemAtIndexPath:(NSIndexPath *)indexPath { static NSString *MyCellIdentifier = @
- (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;
}
我想我的loopIndex有问题。因为当我只使用静态值时。比如说,像这样
cell.textLabel.text = [[_players objectAtIndex:_loopIndex]valueForKey:@"name"];
它不会崩溃。有人知道我如何解决这个问题吗
亲切的问候试试看
_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;
cell=//新的默认单元格;
//为下一次尝试计算_loopindex
if(_loopindex<[[self-getTeam]count])
{
//做好你的工作
}
返回单元;
如果在节和表中设置了正确的行数,则应根据需要准确调用方法
gridView cellForItemAtIndexPath
的次数。不多也不少。这将防止您返回nil或误算\u loopindex
计数器。您误算了\u loopindex
这就是为什么会出现错误的原因,但更重要的是,您应该依赖indexPath
变量,而不是您正在计算的索引来显示此协议方法中的数据。谢谢您的帮助您的答案是正确的,但它仍然会因相同的错误而崩溃。只需正确计算循环索引,然后检查表的长度是否正确。检查网格视图是否被调用了所需的次数。您是否尝试过在其上设置断点并单步执行代码以查看出错的地方?
cell = // new default cell;
// calculate _loopindex for next attempt
if (_loopindex < [[self getTeam]count])
{
//do your job
}
return cell;