Pergunta

I am showing 2 custom cells on my UITableView. I am displaying them as so:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *simpleTableIdentifier = @"StepsViewCell";
    static NSString *simpleTableIdentifier2 = @"descViewCell";

    if( indexPath.row == 0 ) {
        UITableViewCell *cell = nil;
        cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier2];
        if( !cell ) {
                NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"descViewCell" owner:self options:nil];
                cell = [nib objectAtIndex:0];
            }
            cell.textLabel.text = [descLabel objectAtIndex:indexPath.row];

            return cell;
    }
        else {
            StepViewCell *cell = nil;
            cell = (StepViewCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
            if( !cell ) {
                NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"StepsViewCell" owner:self options:nil];
                cell = [nib objectAtIndex:0];
            }

            cell.stepTextLbl.text = [stepLabel objectAtIndex:indexPath.row];
            [cell.thumbImage setImage:[UIImage imageNamed: @"full_breakfast.jpg"] forState:UIControlStateNormal];

            return cell;
            }
        }

I am counting them using the code below as descViewCell is always 1.

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    return [stepLabel count] +1;
}

This works, however I am getting index (5) beyond bounds (5) when i scroll down to the bottom of the UITableView and it crashes. What am I doing wrong?

Foi útil?

Solução

It's because in - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section, you're adding 1 to the result you return. So your stepLabel array has 5 elements (which are at indexes 0 - 4). But when you add 1 to that number, then your code thinks you have 6 rows, which are at indexes 0 - 5. So your - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath method tries to access data from index 5 of an array that only has 5 elements (hence, ends at index 4).

You could make the following change to your code to fix this crash:

cell.stepTextLbl.text = [stepLabel objectAtIndex:indexPath.row-1];

I think that will accomplish what you're trying, because it looks like it only goes into this part of your code if the row is greater than 0. So this shifts the values down by 1, so that you're accessing your array with indices from 0 - 4.

Outras dicas

Your stepLabel count is 5. numberOfRowsInSection: returns 6. This line:

cell.stepTextLbl.text = [stepLabel objectAtIndex:indexPath.row];

crash your app, when indexPath.row = 5, because there is no stepLabel[5]. Try to use

cell.stepTextLbl.text = [stepLabel objectAtIndex:(indexPath.row - 1)];
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top