Ошибка XCode EXC_BAD_ACCESS при использовании sqlite3 + таблица + панель вкладок - PullRequest
0 голосов
/ 31 марта 2012

У меня есть класс ClsDatabase, который вызывается для сохранения / извлечения данных.Есть также 2 вида.Во втором представлении есть таблица, которая будет отображать информацию, основанную на том, что ClsDataBase получил.Панель вкладок управляет 2 представлениями.

Может загружаться с первого показанного изображения.Однако, когда я выбираю 2-й вид, он останавливается на main.m's

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

с сообщением об ошибке: EXC_BAD_ACCESS.

Я попытался создать другой проект, но без ClsDatabase и просто напечатать несколько пустых значений в таблице, и это работает.Не уверен, что это из-за класса 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

(2-й вид.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 голосов
/ 01 апреля 2012

Вы делаете много вещей в коде, которые должны быть сделаны с IB и раскадровкой.

Ваша логика в cellForRowAtIndexPath не будет работать в первом ряду, когда indexPath.row равен 0, а ваш arrDebtor пуст и ваша ячейка еще не создана.Это объясняет ошибку EXC_BAD_ACCESS.

...