【问题标题】:UITableView scrollToRowAtIndexPath: helpUITableView scrollToRowAtIndexPath: 帮助
【发布时间】:2010-07-19 22:19:41
【问题描述】:

我有一个没有设置任何部分的常规 UITableView。我正在尝试像这样自动滚动到给定索引路径处的一行...

[table scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:[self getIndexToShow] inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:NO];

但是当我运行时我得到了这个错误......

2010-07-19 18:07:58.391 Wrecking Ball[413:307] *** Terminating app due to uncaught exception 'NSRangeException', reason: '-[UITableView scrollToRowAtIndexPath:atScrollPosition:animated:]: section (0) beyond bounds (0).'

任何帮助将不胜感激。

段数的委托方法...

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

【问题讨论】:

  • 您的数据源中是否有任何数据,位于该索引路径?
  • 是的,我什至用 0 之类的 int 代替了 getIndexToShow 方法,以防万一我的逻辑出现错误。
  • 如果没有节,怎么会有行?您必须至少有一个部分才能有任何行,并且要滚动到某些内容。您可以发布您的表格视图委托方法的代码吗?
  • 如果你正在使用一个带有UITableView 的Interface Builder 文件,你有没有将delegatedataSource 出口从表视图连接到视图控制器?跨度>
  • 我是用纯代码做的,我正在设置两个代表。除了调用上面提到的方法之外,一切都适用于该表。

标签: iphone objective-c uitableview scroll


【解决方案1】:

这是我的委托方法...

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection: (NSInteger) section
    {
        return [locationLevels count];
    }

- (UITableViewCell *) getCellContentView:(NSString *)cellIdentifier {

    CGRect CellFrame = CGRectMake(0, 0, 447, 210);

    UILabel *levelName = [[UILabel alloc]initWithFrame:CGRectMake(170,6,280,17)];
    UILabel *levelStatus = [[UILabel alloc]initWithFrame:CGRectMake(170,25,280,17)];
    UILabel *levelTime = [[UILabel alloc]initWithFrame:CGRectMake(175,44,300,17)];
    UILabel *levelHeight = [[UILabel alloc]initWithFrame:CGRectMake(175,63,300,17)];
    UILabel *levelScore = [[UILabel alloc]initWithFrame:CGRectMake(175,82,300,17)];

    levelName.font = [UIFont fontWithName:@"Chalkduster" size:17.0f];
    levelStatus.font = [UIFont fontWithName:@"Chalkduster" size:17.0f];
    levelTime.font = [UIFont fontWithName:@"Chalkduster" size:17.0f];
    levelHeight.font = [UIFont fontWithName:@"Chalkduster" size:17.0f];
    levelScore.font = [UIFont fontWithName:@"Chalkduster" size:17.0f];

    levelName.textAlignment = UITextAlignmentCenter;
    levelStatus.textAlignment = UITextAlignmentCenter;

    levelName.backgroundColor = [UIColor clearColor];
    levelStatus.backgroundColor = [UIColor clearColor];
    levelTime.backgroundColor = [UIColor clearColor];
    levelHeight.backgroundColor = [UIColor clearColor];
    levelScore.backgroundColor = [UIColor clearColor];

    levelName.textColor = [UIColor whiteColor]; 
    levelStatus.textColor = [UIColor whiteColor]; 
    levelTime.textColor = [UIColor whiteColor]; 
    levelHeight.textColor = [UIColor whiteColor]; 
    levelScore.textColor = [UIColor whiteColor]; 

    levelName.tag = 1;
    levelStatus.tag = 2;
    levelTime.tag = 3;
    levelHeight.tag = 4;
    levelScore.tag = 5;

    UITableViewCell *cell = [[[UITableViewCell alloc] initWithFrame:CellFrame reuseIdentifier:cellIdentifier] autorelease];

    [[cell contentView] addSubview:levelName];
    [[cell contentView] addSubview:levelStatus];
    [[cell contentView] addSubview:levelTime];
    [[cell contentView] addSubview:levelHeight];
    [[cell contentView] addSubview:levelScore];

    [levelName release];
    [levelStatus release];
    [levelTime release];
    [levelHeight release];
    [levelScore release];

    return cell;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if(cell == nil)
        cell = [self getCellContentView:CellIdentifier];

    if([self levelCanBeShown:indexPath.row])
    {

    GameCard *card = [[[WBManager sharedManager]currentUser]getGameCardForLevel:indexPath.row];

    UILabel *levelName = (UILabel *)[cell viewWithTag:1];
    UILabel *levelStatus = (UILabel *)[cell viewWithTag:2];
    UILabel *levelTime = (UILabel *)[cell viewWithTag:3];
    UILabel *levelHeight = (UILabel *)[cell viewWithTag:4];
    UILabel *levelScore = (UILabel *)[cell viewWithTag:5];

    levelName.text = [self getLevelStringForIntValue:indexPath.row];
    levelStatus.text = card.score <= 0 ? @"Incomplete" : @"Complete";

    if(card.score != 0)
    {
        levelScore.text = [NSString stringWithFormat:@"Score: %i",card.score];
        levelTime.text = [NSString stringWithFormat:@"Best Time: %i mins %i secs",card.time/60,card.time%60];
        levelHeight.text = [NSString stringWithFormat:@"Lowest Height: %i",card.height];
    }

    else
    {
        levelScore.text = @"Score: -";
        levelTime.text = @"BestTime: -";
        levelHeight.text = @"Lowest Height: -";
    }

    UIImage *screenshot = [[UIImage alloc]initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"WSimg%i",indexPath.row+1] ofType:@"PNG"]];

    cell.imageView.image = screenshot;

    [screenshot release];

    }

    else
    {
        UILabel *levelName = (UILabel *)[cell viewWithTag:1];
        UILabel *levelStatus = (UILabel *)[cell viewWithTag:2];
        UILabel *levelTime = (UILabel *)[cell viewWithTag:3];
        UILabel *levelHeight = (UILabel *)[cell viewWithTag:4];
        UILabel *levelScore = (UILabel *)[cell viewWithTag:5];

        levelName.text = [self getLevelStringForIntValue:indexPath.row];
        levelStatus.text = @"LOCKED";
        levelTime.text = @"";
        levelHeight.text = @"";
        levelScore.text = @"";

        UIImage *screenshot = [[UIImage alloc]initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"WSimg%i",indexPath.row+1] ofType:@"PNG"]];

        cell.imageView.image = screenshot;

        [screenshot release];
    }

    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 110;
}

【讨论】:

  • 您能添加您的-numberOfSectionsInTableView: 方法吗?顺便说一句,您应该编辑原始问题以添加代码。这是页面的答案部分。只需点击问题下方的“编辑”即可进行编辑。
  • 看这里的数据源委托方法列表:developer.apple.com/iphone/library/documentation/uikit/…:
  • 我在上面发布了 -numberOfSectionsInTableView:。
【解决方案2】:

您在滚动到特定行之前是否重新加载数据?这是与解决方案相同的问题,我刚刚实施并且运行良好:) uitableview scrolling solution

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-18
    相关资源
    最近更新 更多