2011-10-07 141 views
0

我想填充一个tableView与从SQLite数据库中提取的信息...我已经阅读了一些教程,并试图关注它们,但由于某种原因,我的应用程序不断崩溃。 ..iOS:从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发生在消息发送到已释放的对象时。通过捕捉错误时,调用堆栈通常特别是当处理多个线程不见了。”

好,伟大的..但我不如何解决它...任何帮助?

回答

1

您直接分配到artworkInfo(在viewDidLoad)而不是使用属性访问器来确保数组被正确保留。在调用表视图数据源方法时,您的数组可能会(自动)释放。

应该是:

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

谢谢!似乎做的伎俩:) – user969043

0

如果NSLog(@"%d", rowsInDatabase);向您显示您期望的结果,那么我怀疑artworkInfo没有被保留。假设你已经宣布它这样在您的.h文件中(如果它是一个NSArray而不是一个NSMutableArray或别的东西):

@property (nonatomic, retain) NSArray *artworkInfo; 

那么问题可能是这行:

artworkInfo = [[PaintingDatabase database] findAllArtists]; 

相反尝试将其更改为:

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

不同的是,所述第一线将直接分配变量的值,而第二个实际上是通过一种方法被称为setArtwork运行它信息:尽管看不到它,但在使用@synthesize时会自动创建。由于该属性声明中有retain,因此此方法还将在该对象上调用retain方法,以便在该方法结束后将其保留在内存中。或者,你可以直接做同样的事情这样

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

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