ios – 错误,核心数据,原因:’*** – [_ PFArray objectAtIndex:]:索引(2)超出边界(2)’

ios – 错误,核心数据,原因:’*** – [_ PFArray objectAtIndex:]:索引(2)超出边界(2)’,第1张

概述嗨,我几乎是编程新手. 面对一个我无法解决的错误.甚至在与花药解决方案比较之后. 我已经工作了大约3天. 那么让我完整地描述我的问题: 这是我的实现代码: #import "DocumentTableViewController.h"#import "AddDocumentTableView.h"#import "DB_document.h"@implementation Document 嗨,我几乎是编程新手.
面对一个我无法解决的错误.甚至在与花药解决方案比较之后.
我已经工作了大约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 *lblTotalAmount = [[UILabel alloc] initWithFrame:CGRectMake(165,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,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)’所遇到的程序开发问题。

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

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1053303.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-25
下一篇 2022-05-25

发表评论

登录后才能评论

评论列表(0条)

保存