0

SQLite データベースから抽出した情報を tableView に入力しようとしています...いくつかのチュートリアルを読み、それに従おうとしましたが、何らかの理由でアプリがクラッシュし続けます...

.h

//
//  InOrder.h
//  AGKUnsten
//
//  Created by Jonas Christensen on 7/12/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface InOrder : UIViewController <UITableViewDelegate, UITableViewDataSource> {

    NSArray *artworkInfo;
    int rowsInDatabase;
}

.m

@property (nonatomic, retain) NSArray *artworkInfo;

@end


//
//  InOrder.m
//  AGKUnsten
//
//  Created by Jonas Christensen on 7/12/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "InOrder.h"
#import "ArtworkView.h"
#import "PaintingInfo.h"
#import "PaintingDatabase.h"

@implementation InOrder

@synthesize artworkInfo;

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return rowsInDatabase;
    //return [artworkInfo count]; //Tried to use this, but app just crashes when it reaches this line
}

-(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] autorelease];
    }

    NSLog(@"test");//To see how far I get in the code - this is outputted
    //Configure the cell

    //APP CRASHES IF I TRY TO DO SOMETHING WITH MY DATABASE IN HERE

    //cell.textLabel.text = [artworkInfo objectAtIndex:indexPath.row];//Tried this

    //PaintingInfo *info = [artworkInfo objectAtIndex:indexPath.row];//Tried this
    //cell.textLabel.text = info.artist;

    //[[cell textLabel] setText:[artworkInfo objectAtIndex:[indexPath row]]];//Thread 1: Program received signal: "SIGABRT"

    NSLog(@"test2");//Never reach here if I uncomment any of the above

    return cell;
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)dealloc
{
    [super dealloc];
}

- (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];
    // Do any additional setup after loading the view from its nib.
    self.title = @"Værker";

    artworkInfo = [[PaintingDatabase database] findAllArtists];

    rowsInDatabase = [artworkInfo count];
    NSLog(@"%d", rowsInDatabase);

}

- (void)viewDidUnload
{
    [super viewDidUnload];

    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end

どんな助けでも大歓迎です...他の場所からデータを取得できるので、データベースが機能することは知っていますが、ここで使用しようとするとアプリがクラッシュするようです...

エラーとして表示されるのは主に EXC_BAD_ACCESS と SIGABRT です...

さて、私は SIGABORT = 「SIGABRT は、プログラムが自分自身を中止しようとしているときに送信されるシグナルです。一般的に、これは何か本当に悪いことが起こったために発生します。」と言われました。

「EXC_BAD_ACCESSは、すでに解放されているオブジェクトにメッセージが送信されたときに発生します。エラーがキャッチされるまでに、特に複数のスレッドを処理している場合、コールスタックは通常なくなります。」

まあ、素晴らしい..しかし、私はそれを修正する方法はありません.何か助け??

4

2 に答える 2

1

配列が適切に保持されるようにするプロパティアクセサーを使用する代わりに、artworkInfo(in ) に直接割り当てています。viewDidLoadテーブルビューのデータソースメソッドが呼び出されるまでに、配列はおそらく (自動) 解放されます。

そのはず:

self.artworkInfo = [[PaintingDatabase database] findAllArtists];
于 2011-10-07T01:25:58.743 に答える
0

が期待どおりの結果を示している場合NSLog(@"%d", rowsInDatabase);、artworkInfo が保持されていない可能性があります。.h ファイルでこのように宣言したと仮定します (それが NSArray であり、NSMutableArray などではない場合):

@property (nonatomic, retain) NSArray *artworkInfo;

次に、問題はおそらく次の行です。

artworkInfo = [[PaintingDatabase database] findAllArtists];

代わりに、次のように変更してみてください。

self.artworkInfo = [[PaintingDatabase database] findAllArtists];

違いは、最初の行では変数の値を直接割り当てるのに対し、2 行目では setArtworkInfo: というメソッドを使用して実際に実行することです@synthesize。プロパティ宣言が含まれているため、このメソッドはオブジェクトのメソッドretainも呼び出し、メソッドの終了後もメモリに残ります。retainまたは、このように直接同じことを行うことができます

artworkInfo = [[[PaintingDatabase database] findAllArtists] retain];

また、必ず[artworkInfo release]dealloc メソッドを追加してください。これは、保持しているオブジェクトが終了したら、常に release を呼び出す必要があるためです。

于 2011-10-07T01:29:31.783 に答える