Вопрос

I have 3 custom table cells in my UITableView. The first two cells have set positions followed by a random number of cells.

For my row count I use :

return [stepLabel count] +2;

And for my tablecells I use :

   - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *stepsCellIdentifier = @"StepsViewCell";
    static NSString *descIdentfier = @"descViewCell";
    static NSString *videoCellIdentfier = @"videoViewCell";

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

        return cell;
    }
    else if( indexPath.row == 1 ) {
        stepDescCell *cell = nil;
        cell = (stepDescCell *)[tableView dequeueReusableCellWithIdentifier:descIdentfier];
        if( !cell ) {
                NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"descViewCell" owner:self options:nil];
                cell = [nib objectAtIndex:0];
            }
            cell.descTextLabel.text = [descLabel objectAtIndex:indexPath.row - 1];

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

            cell.stepTextLbl.text = [stepLabel objectAtIndex:(indexPath.row - 2)];
            NSString *imageThumb = [thumbImg objectAtIndex:(indexPath.row - 2)];
            [cell.thumbImage setImage:[UIImage imageNamed: imageThumb] forState:UIControlStateNormal];

            return cell;
            }
        }

All this works perfectly, however when I try to send a string from the table across a segue to another UIViewController I get the following error:

index (-2 (or possibly larger)) beyond bounds (3)'

The prepare for segue method:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:@"imageBigShow"]) {
        NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
        imageBigViewController *destViewController = segue.destinationViewController;
        NSString *largeImgString = [largeImg objectAtIndex:(indexPath.row - 2)];
        destViewController.imageBigString = largeImgString;
    }
}

What am I doing wrong?

UPDATE

@implementation detailViewController
{
    NSArray *thumbImg;
    NSArray *stepLabel;
    NSArray *descLabel;
    NSArray *largeImg;
}

- (void)viewDidLoad

{
    [super viewDidLoad];
    // Find out the path of recipes.plist
    NSDictionary *mapping = @{
                               @"How to wear a Kilt" : @"wearAKilt",
                               @"How to tie a cravat" : @"wearACravat",
                               @"How to wear a sporran" : @"wearASporran",
                               @"How to tie Ghillie Brogues" : @"wearTheShoes",
                               @"How to wear the accessories" : @"wearAccessories",
                               @"How to tie a cravat" : @"wearACravat",
                               @"How to Measure the Neck" : @"htmNeck",
                               @"How to Measure the chest" : @"htmChest",
                               @"How to Measure the waist" : @"htmWaist",
                               };

    NSString *name = [mapping objectForKey:self.title];
    NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:@"plist"];

    // Load the file content and read the data into arrays
    NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:path];
    thumbImg = [dict objectForKey:@"thumbImg"];
    stepLabel = [dict objectForKey:@"stepLabel"];
    descLabel = [dict objectForKey:@"descLabel"];
    largeImg = [dict objectForKey:@"largeImg"];

}
Это было полезно?

Решение

You have the following line:

NSString *largeImgString = [largeImg objectAtIndex:(indexPath.row - 2)];

If the user taps on the 1st or 2nd rows (index paths with row 0 or 1), this code will fail.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top