UITableView bounds终止app'NSRangeException' - [__ NSArrayM objectAtIndex:]:索引2超出bounds [0 .. 1]'

时间:2014-04-07 10:41:31

标签: 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);

    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);

}
 }

请帮帮我。

2 个答案:

答案 0 :(得分:0)

试试这个

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

答案 1 :(得分:0)

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

你有两个元素的数组,并试图引用第三个元素,这在下面的语句中不存在

StudentClass *student = (StudentClass *) [arrayitems objectAtIndex:buttn2.tag];

只需打印数组,您就可以更好地理解错误

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