2012-06-27 114 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]);