1

私は Xcode の初心者で、このエラーを示す UITableViewController を使用しています。

これはエラー メッセージです: *キャッチされない例外 'NSRangeException' が原因でアプリを終了します。理由: ' * -[__NSArrayM objectAtIndex:]: index 1 beyond bounds [0 .. 0] ' 0xc58d5 0xc5b3d 0xacce83 0x1c50376 0x1c4fe06 0x1c37a82 0x1c36f44 0x1c36e1b 0x1beb7e3 0x1beb668 0x1665c 0x2132 0x2065) libc++abi.dylib: 例外をスローして呼び出された DB を終了します

これは私のコードです:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{


 if (indexPath.row == 0) {

    NSString *strURL = [NSString stringWithFormat:@"http://localhost:8888/GetDetail.php?choice=row0"];
    NSArray *arrayImagesNames = [[NSMutableArray alloc] initWithContentsOfURL:[NSURL URLWithString:strURL]];


    arrayDataFromServer2 = [[NSMutableArray alloc]init];

    NSEnumerator *enumForNames = [arrayImagesNames objectEnumerator];

    id objName;

    while ( objName = [enumForNames nextObject]) {
        [arrayDataFromServer2 addObject:[NSDictionary dictionaryWithObjectsAndKeys:objName, @"name", nil]];
    }

    NSString *nn = [[arrayDataFromServer2 objectAtIndex:indexPath.row] objectForKey:@"name"];

    row1 = [nn intValue];
    NSLog(@"%d", row1);

    CompanyProfileViewController *profile = [self.storyboard instantiateViewControllerWithIdentifier:@"Profile"];
    [self.navigationController pushViewController:profile animated:YES];

    profile.profileid = row1;
    NSLog(@"%d", profile.profileid);
}

if (indexPath.row == 1) {

    NSString *strURL = [NSString stringWithFormat:@"http://localhost:8888/GetDetail.php?choice=row1"];
    NSArray *arrayImagesNames = [[NSMutableArray alloc] initWithContentsOfURL:[NSURL URLWithString:strURL]];


    arrayDataFromServer2 = [[NSMutableArray alloc]init];

    NSEnumerator *enumForNames = [arrayImagesNames objectEnumerator];

    id objName;

    while (objName = [enumForNames nextObject]) {
        [arrayDataFromServer2 addObject:[NSDictionary dictionaryWithObjectsAndKeys:objName, @"name", nil]];
    }

    NSString *nn = [[arrayDataFromServer2 objectAtIndex:indexPath.row] objectForKey:@"name"];

    row1 = [nn intValue];
    NSLog(@"%d", row1);

    CompanyProfileViewController *profile = [self.storyboard instantiateViewControllerWithIdentifier:@"Profile"];
    [self.navigationController pushViewController:profile animated:YES];

    profile.profileid = row1;
    NSLog(@"%d", profile.profileid);
}

if (indexPath.row == 2) {

    NSString *strURL = [NSString stringWithFormat:@"http://localhost:8888/GetDetail.php?choice=row2"];
    NSArray *arrayImagesNames = [[NSMutableArray alloc] initWithContentsOfURL:[NSURL URLWithString:strURL]];


    arrayDataFromServer2 = [[NSMutableArray alloc]init];

    NSEnumerator *enumForNames = [arrayImagesNames objectEnumerator];

    id objName;

    while ( objName = [enumForNames nextObject]) {
        [arrayDataFromServer2 addObject:[NSDictionary dictionaryWithObjectsAndKeys:objName, @"name", nil]];
    }

    NSString *nn = [[arrayDataFromServer2 objectAtIndex:indexPath.row] objectForKey:@"name"];

    row1 = [nn intValue];
    NSLog(@"%d", row1);

    CompanyProfileViewController *profile = [self.storyboard instantiateViewControllerWithIdentifier:@"Profile"];
    [self.navigationController pushViewController:profile animated:YES];

    profile.profileid = row1;
    NSLog(@"%d", profile.profileid);
}

if (indexPath.row == 3) {

    NSString *strURL = [NSString stringWithFormat:@"http://localhost:8888/GetDetail.php?choice=row3"];
    NSArray *arrayImagesNames = [[NSMutableArray alloc] initWithContentsOfURL:[NSURL URLWithString:strURL]];


    arrayDataFromServer2 = [[NSMutableArray alloc]init];

    NSEnumerator *enumForNames = [arrayImagesNames objectEnumerator];

    id objName;

    while ( objName = [enumForNames nextObject]) {
        [arrayDataFromServer2 addObject:[NSDictionary dictionaryWithObjectsAndKeys:objName, @"name", nil]];
    }

    NSString *nn = [[arrayDataFromServer2 objectAtIndex:indexPath.row] objectForKey:@"name"];

    row1 = [nn intValue];
    NSLog(@"%d", row1);

    CompanyProfileViewController *profile = [self.storyboard instantiateViewControllerWithIdentifier:@"Profile"];
    [self.navigationController pushViewController:profile animated:YES];

    profile.profileid = row1;
    NSLog(@"%d", profile.profileid);
 }
}

2 番目のセル (index.row == 1) をクリックすると、このエラーが発生します。ブレークポイントを使用しましたが、次の行にエラーがありました: "NSString *nn = [[arrayDataFromServer2 objectAtIndex:indexPath.row] objectForKey:@"name"];"

助けてください!

4

2 に答える 2

3

スイフト4

ストーリーボードからtableViewを使用してコードに実装すると、このエラーが大好きになります。ヒントと比較:セクションの数とnumberOfRowsInSection ==ストーリーボードとコードの間

 override func numberOfSections(in tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 3
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return 1
    }
于 2019-11-23T12:57:49.513 に答える
1

配列があり、その中に 0 または 1 の要素があります。ここで、そこから 2 番目の要素を抽出しようとしています。[0] が最初で、[1] が 2 番目です。

編集:

配列に1つ以上のオブジェクトが含まれる時期がわからないため。したがって、次のように使用できます。

NSString *nn=nil;
if([arrayDataFromServer2 count]>1){    
    nn = [[arrayDataFromServer2 objectAtIndex:indexPath.row] objectForKey:@"name"];
}
于 2013-01-19T15:55:41.240 に答える