Ios 错误,核心数据,原因:'***-[\u PFArray objectAtIndex::]:索引(2)超出边界(2)'

Ios 错误,核心数据,原因:'***-[\u PFArray objectAtIndex::]:索引(2)超出边界(2)',ios,uitableview,core-data,Ios,Uitableview,Core Data,嗨,我几乎是编程新手。 我面临着一个我无论如何都无法解决的错误。即使与花药溶液进行比较。 我已经做了大约3天了 让我完整地描述一下我的问题: 1.这是我的实现代码: #import "DocumentTableViewController.h" #import "AddDocumentTableView.h" #import "DB_document.h" @implementation DocumentTableViewController @synthesize managedObjec

嗨,我几乎是编程新手。 我面临着一个我无论如何都无法解决的错误。即使与花药溶液进行比较。 我已经做了大约3天了

让我完整地描述一下我的问题:

1.这是我的实现代码:

#import "DocumentTableViewController.h"
#import "AddDocumentTableView.h"
#import "DB_document.h"

@implementation DocumentTableViewController

@synthesize managedObjectContext;
@synthesize btnAddDocument;
@synthesize fetchedObjects;

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSManagedObjectContext *context = managedObjectContext;
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"DB_document" inManagedObjectContext:context];
    [fetchRequest setEntity:entity];
    NSError *error;
    fetchedObjects = [context executeFetchRequest:fetchRequest error:&error];
    NSLog(@"%d",[fetchedObjects count]);
}

- (void)viewDidUnload
{
    [self setBtnAddDocument:nil];
    [super viewDidUnload];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    int result = 0;
    if (section == 0) 
    {
        result = [fetchedObjects count] + 1;
    }
    else if (section == 1)
    {
        result = 1;
    }
    return result;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 40;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    DB_document *db_document = [fetchedObjects objectAtIndex:indexPath.row];

    if (indexPath.section == 0 && indexPath.row == 0) 
    {
        UILabel *lblMoney = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 70, 40)];
        lblMoney.text = @"amount";
        lblMoney.textAlignment = UITextAlignmentCenter;
        lblMoney.textColor = [UIColor blackColor];
        lblMoney.backgroundColor = [UIColor clearColor];
        lblMoney.font = [UIFont systemFontOfSize:12];
        [cell addSubview:lblMoney];

        UILabel *lblDescription = [[UILabel alloc] initWithFrame:CGRectMake(85, 0, 150, 40)];
        lblDescription.text = @"description";
        lblDescription.textAlignment = UITextAlignmentCenter;
        lblDescription.textColor = [UIColor blackColor];
        lblDescription.backgroundColor = [UIColor clearColor];
        lblDescription.font = [UIFont systemFontOfSize:12];
        [cell addSubview:lblDescription];

        UILabel *lblDate = [[UILabel alloc] initWithFrame:CGRectMake(240, 0, 70, 40)];
        lblDate.text = @"date";
        lblDate.textAlignment = UITextAlignmentCenter;
        lblDate.textColor = [UIColor blackColor];
        lblDate.backgroundColor = [UIColor clearColor];
        lblDate.font = [UIFont systemFontOfSize:12];
        [cell addSubview:lblDate];

        UIButton *btnLine1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        btnLine1.frame = CGRectMake(80, 0, 1, 40);
        [cell addSubview:btnLine1];

        UIButton *btnLine2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        btnLine2.frame = CGRectMake(240, 0, 1, 40);
        [cell addSubview:btnLine2];

        return cell;
    }
    if (indexPath.section == 0 && indexPath.row != 0)
    {
        UILabel *lblMoney = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 70, 40)];
        lblMoney.text = [NSString stringWithFormat:@"%d",db_document.docAmount];
        lblMoney.textAlignment = UITextAlignmentCenter;
        lblMoney.textColor = [UIColor blackColor];
        lblMoney.backgroundColor = [UIColor clearColor];
        lblMoney.font = [UIFont systemFontOfSize:12];
        [cell addSubview:lblMoney];

        UILabel *lblDescription = [[UILabel alloc] initWithFrame:CGRectMake(85, 0, 150, 40)];
        lblDescription.text = db_document.docDescription;
        lblDescription.numberOfLines = 2;
        lblDescription.textAlignment = UITextAlignmentCenter;
        lblDescription.textColor = [UIColor blackColor];
        lblDescription.backgroundColor = [UIColor clearColor];
        lblDescription.font = [UIFont systemFontOfSize:12];
        [cell addSubview:lblDescription];

        UILabel *lblDate = [[UILabel alloc] initWithFrame:CGRectMake(240, 0, 70, 40)];
        NSDateFormatter *dateFormater = [[NSDateFormatter alloc] init];
        [dateFormater setDateFormat:@"yyyy/mm/dd"];
        lblDate.text = [NSString stringWithFormat:@"%@",[dateFormater stringFromDate:(NSDate *)db_document.docDate]];
        lblDate.textAlignment = UITextAlignmentCenter;
        lblDate.textColor = [UIColor blackColor];
        lblDate.backgroundColor = [UIColor clearColor];
        lblDate.font = [UIFont systemFontOfSize:12];
        [cell addSubview:lblDate];

        UIButton *btnLine1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        btnLine1.frame = CGRectMake(80, 0, 1, 40);
        [cell addSubview:btnLine1];

        UIButton *btnLine2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        btnLine2.frame = CGRectMake(240, 0, 1, 40);
        [cell addSubview:btnLine2];

        return cell;
    }
    if (indexPath.section == 1)
    {
        UILabel *lblMoney = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 70, 40)];
        lblMoney.text = @"";
        lblMoney.textAlignment = UITextAlignmentCenter;
        lblMoney.textColor = [UIColor blackColor];
        lblMoney.backgroundColor = [UIColor clearColor];
        lblMoney.font = [UIFont systemFontOfSize:12];
        [cell addSubview:lblMoney];

        UILabel *lblTotalAmount = [[UILabel alloc] initWithFrame:CGRectMake(165, 0, 140, 40)];
        lblTotalAmount.text = @"amounts";
        lblTotalAmount.textAlignment = UITextAlignmentCenter;
        lblTotalAmount.textColor = [UIColor blackColor];
        lblTotalAmount.backgroundColor = [UIColor clearColor];
        lblTotalAmount.font = [UIFont systemFontOfSize:12];
        [cell addSubview:lblTotalAmount];

        UIButton *btnLine = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        btnLine.frame = CGRectMake(160, 0, 1, 40);
        [cell addSubview:btnLine];

        return cell;
    }
    return cell;
}

- (IBAction)btnAddDocument_click:(id)sender
{
    AddDocumentTableView *addDocumentTableView = [[AddDocumentTableView alloc] init];
    addDocumentTableView.managedObjectContext = managedObjectContext;
    [self.navigationController pushViewController:addDocumentTableView animated:YES];
}
2.这是错误:

2012-06-16 15:25:31.696会计5[5534:fb03]2 2012-06-16 15:25:31.704 Account5[5534:fb03]*由于未捕获的异常“NSRangeException”而终止应用程序,原因:“*-[\u PFArray objectAtIndex:]:索引2超出边界2” ***第一次抛出调用堆栈:

3.让我描述一下计划。我可以用核心数据将数据保存到数据库,但是当我想取出数据程序时,我必须考虑到我认为NSMARMARDION对象上下文获取数据是因为FixDeDebug对象数组有2个数据,因为我插入了。 我必须说,我的RootViewController是DocumentTableViewController,这意味着当我运行程序时,它会崩溃。如果我想运行这个应用程序,我必须注释DB_document*DB_document=[fetchedObjects objectAtIndex:indexPath.row]

在应用程序运行之后,我可以在另一个页面中插入数据。 我必须考虑到,当应用程序崩溃时,它完全停止在

DB_document*DB_document=[fetchedObjects objectAtIndex:indexPath.row]

用绿色突出显示的线条。 谢谢

这是您的问题:

在numberOfRowsInSection中,向fetchedResults数组添加一个。您可能希望在该部分中添加一行。那很好

但是,在cellForRowAtIndexPath中,您将indexath.row作为db_文档的索引。显然,在最后一排它会崩溃。您必须首先检查您所在的行,然后仅当您需要该行的db_文档时才检索它

问题在于:

result = [fetchedObjects count] + 1; 
关于截面上的行数 在cellForRowAtIndexPath上添加的行数超过了FetchedObject的计数
例如,FetchedObject有第3行,而您有第4行,此异常被忽略,您在该数组上超出了范围。

您发布的代码太多了。请选择您认为有责任的代码部分并删除其余部分。谢谢朋友。我完全糊涂了。再次感谢你抽出时间。你刚刚让我自由了我在[numberOfRowsInSection]中删除了代码中的+1,但它并不能完全解决我的问题。我的桌子缺一排!!!这意味着,如果我有3个数据,我的表将显示2个数据,实际上正如您所说,我想添加一个额外的行,但我不知道如何添加?保留+1并检查您在cellForRowAtIndexPath中的行。只有在确定索引未超出数组大小时,才查询数组。