0

我有一個ClsDatabase類被調用來保存/檢索數據。 還有2意見。第二個視圖的表格將顯示基於ClsDataBase檢索的信息。一個標籤欄控制2個視圖。當使用sqlite3 +表+標籤欄時XCode錯誤EXC_BAD_ACCESS

它可以在顯示的第一個視圖初始加載。然而,當我選擇第二個觀點,它停在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 

(第二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; 
    } 
} 
+0

當我嘗試使用NSZombieEnabled來跟蹤這種類型的錯誤時,應用程序剛剛退出殭屍消息之前,當我點擊第二個選項卡。 – 2012-03-31 10:38:41

+0

您發佈的代碼太多。儘量減少它,只顯示你懷疑可能包含問題的部分。 – Mundi 2012-03-31 21:28:46

+0

是。已經減少到最低限度...對不起。 – 2012-04-01 09:08:47

回答

0

你在應用IB和storyboarding完成的代碼中做了很多事情。

你在cellForRowAtIndexPath邏輯將失敗的第一行上,當indexPath.row0和你arrDebtor是空的,你的細胞尚未建立。這將解釋EXC_BAD_ACCESS錯誤。