2012-06-27 164 views
1

我是一名新的iPad開發人員。將整數添加到NSMUtableArray

我實現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]; 
... 
+0

我認爲拋出的異常是該行的NSLog(@ 「沒有=%@」,[remindarray objectAtIndex:ⅰ]); – janusbalatbat

回答

5

您的循環開始從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]]; 
+0

當我寫'cell.textLabel.text = [remindarray objectAtIndex:indexPath.row];'中的tableview我的應用程序崩潰。 – Krunal

+0

@krunal檢查更新回答 –

+0

選擇索引的表?取它在哪裏? –

2

由於新創建的數組指數從0指數沒有開始從3

的問題是在這個循環

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

當我寫'cell.textLabel.text = [alertsarray objectAtIndex:indexPath.row];'在tableview我的應用程序崩潰。 – Krunal

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

在這裏,你用3和i開始要指定單個對象在第一個remindarray所以它僅包含一個對象,以便objectAtIndex:3將是零所以莫dify這樣

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

NSLog(@"no=%@",[remindarray objectAtIndex:0]); 
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]); 
} 
1

解決方法很簡單:你的索引0添加對象數組,然後你要在索引打印的對象3.使用 :

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