1

私は新しい iPad 開発者です。

ボタンのクリック時に UIPopover を実装しており、ポップオーバーには整数値が含まれています。

配列に整数を入力しようとすると、次のように表示されますSIGABRT:境界を超えたインデックス 3ログが表示されず、アプリがクラッシュします。

ここに私のコードスニペットがあります:

-(void)btnClick:(id)sender {

    for (int i=3; i<=31; i++) {
        [remindarray addObject:[NSNumber numberWithInteger:i]];
         NSLog(@"no=%@",[remindarray objectAtIndex:i]);
    }

    UIViewController* popoverContent = [[UIViewController alloc]init];
    UIView* popoverView = [[UIView alloc] initWithFrame:CGRectMake(110, 0, 500, 4)];

    popoverTable = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 250, 665) style:UITableViewStylePlain];
    [popoverTable setDelegate:(id<UITableViewDelegate>)self]; 
    [popoverTable setDataSource:(id<UITableViewDataSource>)self]; 
    [self.view addSubview:popoverTable];
    [popoverTable release];

    [popoverView addSubview:popoverTable];
    popoverContent.view = popoverView;
    popoverContent.contentSizeForViewInPopover = CGSizeMake(250, 600);
    self.popoverController = [[UIPopoverController alloc]
                              initWithContentViewController:popoverContent];

    [self.popoverController  presentPopoverFromRect:CGRectMake(100,0, 535, 35) 
                                             inView:self.view permittedArrowDirections:UIPopoverArrowDirectionRight animated:YES];

    [popoverView release];
    [popoverContent release];
}

そして最後にremind array私はに渡しますcellForRowAtIndexPath

コード:

...
cell.textLabel.text=[remindarray objectAtIndex:indexPath.row];
...
4

5 に答える 5

5

forループは3から始まるため、配列では0アイテムで始まり、1アイテムを挿入して、まだ配列内にないインデックス3でアイテムをログに記録しようとします。

簡単な修正は

変化する

NSLog(@"no=%@",[remindarray objectAtIndex:i]);

NSLog(@"no=%@",[remindarray objectAtIndex:i - 3]);

または、0から配列を開始します

また、変更する必要があります

cell.textLabel.text=[remindarray objectAtIndex:indexPath.row];

cell.textLabel.text=[NSString stringWithFormat:@"%@", [remindarray objectAtIndex:indexPath.row]];
于 2012-06-27T12:29:09.690 に答える
2

新しく作成された配列インデックスは、からで0はなくインデックスから開始されたため3

問題はこのループにあります

for (int i=3; i<=31; i++) {
        [remindarray addObject:[NSNumber numberWithInteger:i]];
         NSLog(@"no=%@",[remindarray objectAtIndex:i]);
    }
于 2012-06-27T12:29:22.587 に答える
1
for (int i=3; i<=31; i++) {
    [remindarray addObject:[NSNumber numberWithInteger:i]];
     NSLog(@"no=%@",[remindarray objectAtIndex:i]);
}

ここでは、i を 3 で開始し、reminderarray に単一のオブジェクトを割り当てているため、最初はオブジェクトが 1 つしか含まれていないため、objectAtIndex:3 は nil になるため、次のようにコードを変更します。

NSLog(@"no=%@",[remindarray objectAtIndex:i-3]);

また

NSLog(@"no=%@",[remindarray objectAtIndex:0]);
于 2012-06-27T12:30:11.883 に答える
1

例外をスローしたのはこの行だと思います

NSLog(@"no=%@",[remindarray objectAtIndex:i]);

//
-(void)btnClick:(id)sender {
for (int i=3; i<=31; i++) {
    [remindarray addObject:[NSNumber numberWithInteger:i]];
    //your remindArray has one object, index is 0, but you are accessing the 
    //object which is at the index of 3, but the remindArray
    // has only one object [0]
     NSLog(@"no=%@",[remindarray objectAtIndex:i]);
}
于 2012-06-27T12:34:59.800 に答える
1

解決策は簡単です。配列のインデックス 0 にオブジェクトを追加してから、インデックス 3 にあるオブジェクトを出力します。次を使用します。

NSLog(@"no=%@",[remindarray objectAtIndex:i - 3]);
于 2012-06-27T12:41:49.127 に答える