UITableView scrollToRowAtIndexPath:帮助

时间:2010-07-19 22:19:42

标签: iphone objective-c uitableview scroll

我有一个常规的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;
}

2 个答案:

答案 0 :(得分:0)

以下是我的委托方法......

-(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;
}

答案 1 :(得分:0)

你在滚动到特定行之前是否重新加载数据?这是与slution相同的问题,我刚刚实施,它运作良好:) uitableview scrolling solution