gpt4 book ai didi

ios - 错误,核心数据,原因: '*** -[_PFArray objectAtIndex:]: index (2) beyond bounds (2)'

转载 作者:可可西里 更新时间:2023-11-01 05:38:04 24 4
gpt4 key购买 nike

大家好,我几乎是编程新手。我遇到了一个无论如何都无法解决的错误。即使在与花药解决方案进行比较之后。我已经为此工作了大约 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] 22012-06-16 15:25:31.704 Account5[5534:fb03] * 由于未捕获的异常 'NSRangeException' 而终止应用程序,原因:'* -[_PFArray objectAtIndex:]: index (2) beyond边界(2)'*** 首先抛出调用栈:

3.让我描述一下程序。我可以使用核心数据将数据保存到数据库,但是当我想获取数据时,程序跳出。我必须考虑我认为 NSManagedObjectContext 获取了数据,因为 fetchedObjects 数组有 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:]: index (2) beyond bounds (2)' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11063209/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com