通过segue时索引超出范围

时间:2014-02-03 16:36:09

标签: ios uistoryboardsegue nsindexpath

我的UITableView中有3个自定义表格单元格。前两个单元格设置了位置,后跟随机数量的单元格。

对于我的行数,我使用:

return [stepLabel count] +2;

对于我的tablecells,我使用:

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

所有这一切都很完美,但是当我尝试将表中的字符串从segue发送到另一个UIViewController时,我收到以下错误:

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

准备segue方法:

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

我做错了什么?

更新

@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"];

}

1 个答案:

答案 0 :(得分:1)

您有以下一行:

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

如果用户点击第1行或第2行(带有行0或1的索引路径),则此代码将失败。