【问题标题】:iOS: filling tableView from SQLite databaseiOS:从 SQLite 数据库填充 tableView
【发布时间】:2011-12-02 16:29:38
【问题描述】:

我正在尝试使用从 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 发生在消息被发送到已经释放的对象时。到错误被捕获时,调用堆栈通常已经消失,尤其是在处理多个线程时。”

好吧,太好了..但我不知道如何解决它...有什么帮助吗??

【问题讨论】:

    标签: ios sqlite uitableview exc-bad-access sigabrt


    【解决方案1】:

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

    应该是:

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

    【讨论】:

      【解决方案2】:

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

      并确保在您的 dealloc 方法中添加 [artworkInfo release],因为您应该始终在使用完保留的任何对象时调用 release。

      【讨论】:

        猜你喜欢
        • 2011-03-22
        • 1970-01-01
        • 1970-01-01
        • 2016-04-21
        • 1970-01-01
        • 1970-01-01
        • 2016-10-24
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多