我正在尝试使用从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在将消息发送到已经释放的对象时发生。当捕获到错误时,调用堆栈通常会消失,尤其是在处理多个线程时。”
嗯,很棒..但我不知道如何解决它...任何帮助?
答案 0 :(得分:1)
您直接分配给artworkInfo
(在viewDidLoad
中)而不是使用属性访问器,这将确保正确保留数组。在调用表视图数据源方法时,您的数组可能(自动)发布。
应该是:
self.artworkInfo = [[PaintingDatabase database] findAllArtists];
答案 1 :(得分:0)
如果NSLog(@"%d", rowsInDatabase);
显示您期望的结果,那么我怀疑是不保留artworkInfo。假设你已经在.h文件中以这种方式声明它(如果它是NSArray而不是NSMutableArray或其他东西):
@property (nonatomic, retain) NSArray *artworkInfo;
然后问题可能是这一行:
artworkInfo = [[PaintingDatabase database] findAllArtists];
而是尝试将其更改为:
self.artworkInfo = [[PaintingDatabase database] findAllArtists];
不同之处在于第一行将直接赋值变量值,而第二行实际上通过一个名为setArtworkInfo:的方法运行它,虽然你看不到它,但是当你使用{{1 }}。因为属性声明中包含@synthesize
,所以此方法还将调用对象上的retain
方法,以便在方法结束后保留在内存中。或者,你可以像这样直接做同样的事情
retain
并确保在dealloc方法中添加artworkInfo = [[[PaintingDatabase database] findAllArtists] retain];
,因为在完成任何对象时,应始终调用release。