4

こんにちは、プログラミング初心者です。とにかく解決できないエラーに直面しています。他のソリューションと比較した後でも。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 Account5[5534:fb03] 2 2012-06-16 15:25:31.704 Account5[5534:fb03] * キャッチされない例外 'NSRangeException' によるアプリの終了、理由: '* -[ _PFArray objectAtIndex:]: index (2) beyond bounds (2)' *** 最初のスロー コール スタック:

3.プログラムについて説明します。コアデータでデータベースにデータを保存することはできますが、データを取得しようとするとプログラムが飛び出してしまいます。 RootViewController は DocumentTableViewController です。これは、プログラムを実行するとクラッシュすることを意味します。アプリを実行したい場合は、コメントする必要がありますDB_document *db_document = [fetchedObjects objectAtIndex:indexPath.row];

その後、アプリが実行され、別のページにデータを挿入できます。アプリがクラッシュすると、正確に停止することを考慮する必要があります

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

緑色の強調表示された線で。ありがとうございました

4

2 に答える 2

5

これがあなたの問題です:

配列にnumberOfRowsInSection1 つ追加します。fetchedResultsおそらく、そのセクションに行を追加したいと思うでしょう。それは結構です。

ただし、あなたcellForRowAtIndexPathの. 明らかに、最後の行でクラッシュします。最初に自分がどの行にいるかを確認してから、その特定の行に必要な場合にのみ取得する必要があります。indexPath.rowdb_documentdb_document

于 2012-06-16T12:15:12.593 に答える
0

問題はここにあります:

result = [fetchedObjects count] + 1; 

numberOfRowsInSection では、fetchedObjects カウントよりも 1 行多く追加します。cellForRowAtIndexPath では、fetchedObjects にたとえば 3 行あり、行 4 を取得すると、この例外が発生し、この配列の範囲外になります。

于 2012-07-01T11:16:53.273 に答える