HTML5   发布时间:2022-04-27  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了ios – 错误,核心数据,原因:’*** – [_ PFArray objectAtIndex:]:索引(2)超出边界(2)’大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
嗨,我几乎是编程新手.
面对一个我无解决错误.甚至在与花药解决方案比较之后.
我已经工作了大约3天.

那么让我完整地描述我的问题:

这是我的实现代码

#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,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,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,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,1,40);
        [cell addSubview:btnLine1];

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

        return cell;
    }
    if (indexPath.section == 0 && indexPath.row != 0)
    {
        UILabel *lblMoney = [[UILabel alloc] initWithFrame:CGRectMake(10,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,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,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,40);
        [cell addSubview:btnLine2];

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

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

        UIButton *btnLine = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        btnLine.frame = CGRectMake(160,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];
}

这是错误

2012-06-16 15:25:31.696 Account5 [5534:fb03] 2
2012-06-16 15:25:31.704 Account5 [5534:fb03] *由于未捕获的异常’NSRangeException’终止应用程序,原因:’* – [_ PFArray objectATindex:]:index(2)超出bounds(2)’
***第一次抛出调用堆栈:

让我来描述这个节目.我可以使用核心数据将数据保存到数据库但是当我想要获取数据时跳出来.我必须虑我认为NsmanagedObjectContext获取数据因为fetchedObjects Array有2个数据,因为我插入了.
我不得不说我的RootViewController是DocumentTableViewController它意味着当我运行程序时它崩溃了.如果我想运行应用程序,我必须注释DB_document * db_document = [fetchedObjects objectATindex:indexPath.row];

然后该应用程序运行,我可以在另一个页面插入数据.
我必须虑当应用程序崩溃时它会完全停止

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

绿色突出显示的行.
谢谢

解决方法

这是你的问题:

numberOfRowsInSection中,向fetchedResults数组添加一个.据推测,您希望在该部分中添加其他行.那样就好.

但是,在cellForRowATindexPath中,您将indexPath.row作为db_document的索引.显然,在最后一行它会崩溃.您必须首先检查您是哪一行,然后仅在您需要该特定行时才检索db_document.

大佬总结

以上是大佬教程为你收集整理的ios – 错误,核心数据,原因:’*** – [_ PFArray objectAtIndex:]:索引(2)超出边界(2)’全部内容,希望文章能够帮你解决ios – 错误,核心数据,原因:’*** – [_ PFArray objectAtIndex:]:索引(2)超出边界(2)’所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。