views:

93

answers:

1

I have a regular UITableView without any sections set up. I'm trying to automatically scroll to a row at a given index path like so...

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

But when ran I get this error...

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).'

Any help would be appreciated.

Delegate method for number of sections...

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

Here are my delegate methods...

-(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;
}
Avizzv92
Can you add your `-numberOfSectionsInTableView:` method?As as aside, you should edit your original question to add code. This is the answer part of the page. Just click on "edit" underneath your question to edit it.
Alex Reynolds
Take a look at the data source delegate method list here: http://developer.apple.com/iphone/library/documentation/uikit/reference/UITableViewDataSource_Protocol/Reference/Reference.html#//apple_ref/occ/intfm/UITableViewDataSource/numberOfSectionsInTableView:
Alex Reynolds
I posted the -numberOfSectionsInTableView: up above.
Avizzv92