Ios UITableView应用程序';nsrange异常'-[u NSArrayM objectAtIndex:]:索引2超出界限[0..1]';

Ios UITableView应用程序';nsrange异常'-[u NSArrayM objectAtIndex:]:索引2超出界限[0..1]';,ios,uitableview,nsmutablearray,nsrangeexception,Ios,Uitableview,Nsmutablearray,Nsrangeexception,以下代码有问题:我有一个LeavemanagementViewController.m 我正在从服务器提取和加载xml数据。元素的总数取决于数组 -(void)RejectActionPressed:(id)sender { if ([approve1role.text isEqualToString:datarole.text]) { UIButton *buttn2=(UIButton*)sender; NSLog(@"bttn.tag==%i",buttn2.tag);

以下代码有问题:我有一个LeavemanagementViewController.m

我正在从服务器提取和加载xml数据。元素的总数取决于数组

-(void)RejectActionPressed:(id)sender
{
if ([approve1role.text isEqualToString:datarole.text])
{
    UIButton *buttn2=(UIButton*)sender;
    NSLog(@"bttn.tag==%i",buttn2.tag);

    StudentClass *student = (StudentClass *) [arrayitems objectAtIndex:buttn2.tag];
    NSLog(@"student name= %@ \n leavecode %@ \n joining dat=%@",student.employeeName,student.leavecode,student.joiningdate);

    UIAlertView *alert1 = [[UIAlertView alloc] initWithTitle:@"Alert" message:@" " delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok", nil];
    alert1.tag = buttn2.tag;
    alert1.tag = kAlertViewTwo; CGRect frame = CGRectMake(14, 45, 255, 23);
    remarkstext = [[UITextField alloc] initWithFrame:frame];
    remarkstext.placeholder = @"Name";
    remarkstext.backgroundColor = [UIColor whiteColor];
    remarkstext.autocorrectionType = UITextAutocorrectionTypeDefault;
    remarkstext.keyboardType = UIKeyboardTypeAlphabet;
    remarkstext.returnKeyType = UIReturnKeyDone;
    remarkstext.clearButtonMode = UITextFieldViewModeWhileEditing; 
    [alert1 addSubview:remarkstext];
    [alert1 show];
 }
 - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
     {
 if(alertView.tag == kAlertViewOne)
{
    if (buttonIndex==1)
       {
         NSLog(@"%@",remarkstext.text);
         remarksoutput.text = remarkstext.text;
         NSLog(@" The Alertview%@",remarksoutput.text);
           [self callWebservice:alertView.tag];
       }
     else if (buttonIndex==0)
      {
       NSLog(@"cancel");
      }
  }
if(alertView.tag == kAlertViewTwo)
{
    if (buttonIndex==1)
    {
        NSLog(@"%@",remarkstext.text);

        remarksoutput.text = remarkstext.text;
         NSLog(@" The Alertview%@",remarksoutput.text);
        [self callRejectWebservice:alertView.tag];
    }
    else if (buttonIndex==0)
    {
        NSLog(@"cancel");
   }
}
   }

  -(void)callRejectWebservice:(int)tag
    {
    if ([approve1role.text isEqualToString:datarole.text])
{
    StudentClass *student = (StudentClass *) [arrayitems objectAtIndex:tag];  // Here is my error *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 2 beyond bounds [0 .. 1]'           
    NSLog(@"student name= %@ \n leavecode %@ \n joining dat=%@",student.employeeName,student.leavecode,student.joiningdate);
}
if ([approve2role.text isEqualToString:datarole.text])
{

    StudentClass *student = (StudentClass *) [arrayitems objectAtIndex:tag];
    NSLog(@"student name= %@ \n leavecode %@ \n joining dat=%@",student.employeeName,student.leavecode,student.joiningdate);

}
if ([approve3role.text isEqualToString:datarole.text])
{
    StudentClass *student = (StudentClass *) [arrayitems objectAtIndex:tag];
    NSLog(@"student name= %@ \n leavecode %@ \n joining dat=%@",student.employeeName,student.leavecode,student.joiningdate);

}
 }
请帮我解决这个问题。

试试这个

StudentClass *student = (StudentClass *) [arrayitems objectAtIndex:tag-1];

它看起来像是一个通用数组越界问题

您有一个包含两个元素的数组,并且正在尝试引用第三个元素,这在下面的语句中不存在

StudentClass *student = (StudentClass *) [arrayitems objectAtIndex:buttn2.tag];
只需打印数组,就可以更好地了解错误

NSLog(@"Array =%@",arrayitems); //to debug

如果我添加了上述代码,0 indexpath显示indexpath 1值的值。您想告诉我Button2的值是多少吗。Tag我在tablecell//创建按钮并将其添加到单元格-拒绝按钮UIButton*Button2=[UIButton button WithType:UIButtonTypeUnderRect];button2.tag=indexPath.row;[button2 addTarget:self action:@selector(RejectActionPressed:)forControlEvents:UIControlEventTouchDown];button2.frame=CGRectMake(720.0f、85.0f、100.0f、25.0f);[单元格添加子视图:按钮2];我认为错误可能在alert1.tag=kAlertViewTwo;,您将替换alert1.tag。首先,您将设置为alert1.tag=button2.tagNo,我声明了单独的buttn alloc,但我按照自己的方式进行了尝试。我得到了相同的错误。数组计数是2,我使用数组值获取。我需要相同的indexath-(void)callRejectWebservice:(int)标记在此条件下数组计数2表示“数组有两个元素数组[0],数组[1]”。您正在引用数组[2],该数组不存在。所以它的给出是错误的。你能在上面的代码中指出你的评论吗。让我知道我在哪里做了错误的StudentClass*student=(StudentClass*)[arrayitems objectAtIndex:buttn2.tag-1]。试试这个!!你能告诉我我需要添加我在-(void)RejectActionPressed:(id)sender方法中添加的bcz吗?我收到了相同的错误