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];

不同之处在于第一行将直接分配变量的值,而第二行实际上通过一个名为 setArtworkInfo: 的方法运行它,虽然您看不到它,但在您使用时会自动创建@synthesize。因为里面有属性声明retain,所以这个方法也会调用retain对象上的方法,这样在方法结束后就留在内存中。或者,您可以像这样直接做同样的事情

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

并确保添加[artworkInfo release]你的 dealloc 方法,因为你应该总是在你保留的任何对象上调用 release,当你完成它们时。

于 2011-10-07T01:29:31.783 回答