2012-06-16 35 views
4

嗨,我幾乎是編程中的新手。 我面臨着一個我無法解決的錯誤。即使在與其他解決方案比較後。 我已經爲它工作了大約3天。錯誤,核心數據,原因:'*** - [_ PFArray objectAtIndex:]:index(2)beyond bounds(2)'

因此,讓我完全說明我的問題:

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 Account5 [5534:fb03] 2 2012-06-16 15:25:31.704 Account5 [5534:fb03] *由於未捕獲異常'NSRangeException',原因:'* - [_ PFArray objectAtIndex:]:指數(2)越界(2)' ***第一次擲出呼叫堆棧:

3.讓我描述程序。我可以使用核心數據將數據保存到數據庫中,但是當我想要獲取數據程序時會跳出。我必須考慮我認爲NSManagedObjectContext獲取數據,因爲fetchedObjects數組有2個數據,因爲我插入了。 ,我不得不說我的RootViewController是DocumentTableViewController,它意味着,當我運行程序時它崩潰。如果我想運行應用程序,我必須發表評論DB_document *db_document = [fetchedObjects objectAtIndex:indexPath.row];

之後,應用程序運行,我可以插入數據在另一個頁面。 我要考慮的是,當應用程序崩潰,停止對究竟

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

與突出顯示爲綠色線。 謝謝

+1

您張貼的方式太多的代碼。請選擇您認爲有責任的代碼部分,然後刪除其餘部分。 – Mundi

回答

5

這裏是你的問題:

numberOfRowsInSection你添加一個到fetchedResults陣列。據推測,你想在該部分添加一行。沒事兒。

但是,在cellForRowAtIndexPath中,您將indexPath.row作爲db_document的索引。顯然,在最後一行它會崩潰。您必須先檢查您所在的行,然後僅在您需要該行時檢索您的db_document

+0

謝謝你的朋友。 我完全困惑。 再次感謝花時間。 你剛剛讓我自由。 :)) –

+0

我已經從[codeOfRowsInSection]的代碼中刪除了+1,但它並沒有完全解決我的問題。 我的桌子有一排排! 這意味着如果我有3個數據我的表將顯示2個數據(實際上,正如你所說我想添加一個額外的行殭屍我不知道如何?) –

+0

保持+1,並檢查你在' cellForRowAtIndexPath'。只有在確定索引不超出數組大小時才查詢數組。 – Mundi

0

問題是在這裏:

result = [fetchedObjects count] + 1; 

上numberOfRowsInSection 添加一行超過fetchedObjects計數,當你在的cellForRowAtIndexPath 的fetchedObjects已經例如3行,你會得到第4行,而這個例外occored並且你在這個數組上超出範圍。

相關問題