使用sqlite3 + table + tab栏时XCode错误EXC_BAD_ACCESS

时间:2012-03-31 10:31:59

标签: xcode uitableview uitabbarcontroller xcode4.2 exc-bad-access

我有一个用于保存/检索数据的类ClsDatabase。 还享有2个景致。第二个视图具有将根据ClsDataBase检索的信息显示信息的表。标签栏控制2个视图。

最初可以显示第一个视图。但是,当我选择第二个视图时,它会在main.m的

处停止
return UIApplicationMain(argc, argv, nil, NSStringFromClass([TestTabBarAppDelegate class]));

带有错误消息:EXC_BAD_ACCESS。

我尝试创建另一个项目,但没有ClsDatabase,只是在表格中打印一些dummmy值,它可以工作。不确定它是否因为类ClsDatabase

AppDelegate.m

#import "TestTabBarAppDelegate.h"
#import "TestTabBarFirstViewController.h"
#import "TestTabBarSecondViewController.h"

@implementation TestTabBarAppDelegate

@synthesize window = _window;
@synthesize tabBarController = _tabBarController;

- (void)dealloc
{
    [_window release];
    [_tabBarController release];
    [super dealloc];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    UIViewController *viewController1 = [[[TestTabBarFirstViewController alloc] initWithNibName:@"TestTabBarFirstViewController" bundle:nil] autorelease];
    UIViewController *viewController2 = [[[TestTabBarSecondViewController alloc] initWithNibName:@"TestTabBarSecondViewController" bundle:nil] autorelease];
    self.tabBarController = [[[UITabBarController alloc] init] autorelease];
    self.tabBarController.viewControllers = [NSArray arrayWithObjects:viewController1, viewController2, nil];
    self.window.rootViewController = self.tabBarController;
    [self.window makeKeyAndVisible];
    return YES;
}

(第2个观点).h

#import <UIKit/UIKit.h>

#import "ClsDatabase.h"

@interface TestTabBarSecondViewController : UIViewController<UIApplicationDelegate, UITabBarControllerDelegate>{
    NSArray *arrDebtor;
    ClsDatabase *dbDebtor;
}

@end

(2nd view.m)

#import "TestTabBarSecondViewController.h"

@implementation TestTabBarSecondViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = NSLocalizedString(@"Second", @"Second");
        self.tabBarItem.image = [UIImage imageNamed:@"second"];
    }
    return self;
}

- (void)viewDidLoad
{    
    NSString *strSql;
    dbDebtor = [[ClsDatabase alloc] initWithDbName:@"Debt"];
    arrDebtor = [[NSArray alloc] init];

    strSql = [NSString stringWithFormat:@"SELECT * FROM Debtor"];
    arrDebtor = [dbDebtor ReturnQueryArray:strSql];

    [super viewDidLoad];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section             {
    if ([arrDebtor count] == 0) {
        return 1;
    }
    else {
        return [arrDebtor count];
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{  
    static NSString *cellIdentifier = @"Debtor";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    if ([arrDebtor count] == 0) {
        if (indexPath.row == 1) {
            if (cell == nil) 
            {
                cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease];
        }
            cell.textLabel.text  = [NSString stringWithFormat:@"No tables or records found"];
            return cell;
        }
    }
    else {
        if (cell == nil) 
        {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease];
        }
        NSMutableDictionary *dictDebtor = [arrDebtor objectAtIndex:indexPath.row];
        cell.textLabel.text  = [dictDebtor valueForKey:@"Name"];
        return cell;
    }
}

1 个答案:

答案 0 :(得分:0)

你在代码中做了很多事情,应该用IB和故事板来完成。

cellForRowAtIndexPath中的indexPath.row中的逻辑将在第一行失败,0arrDebtor且您的{{1}}为空并且您的单元格尚未创建。这可以解释EXC_BAD_ACCESS错误。