Hi,
could someone please help me? I've been trying to create a own Database class for my TableViews so I don't have to open my Database each time I have a new TableView.
In my Database class i have a method which takes a NSString, the sql query and returns an NSMutableArray.
When I then open my TableView the first time, everything works fine, but when i start scrolling the data seems to be gone. And my App crashes.
With NSZombies i get this Error:
-[StateTableViewController tableView:cellForRowAtIndexPath:]: message sent to deallocated instance 0x5c6a5c0
Please help me!
Database.h
Code:
#import <Foundation/Foundation.h>
#import <sqlite3.h>
@interface Database : NSObject {
NSMutableArray *result;
sqlite3 *database;
sqlite3_stmt *statement;
}
+ (Database *)sharedDatabase;
- (NSMutableArray *)query:(NSString *)stmt;
@end
Database.m
Code:
#import "Database.h"
static Database *sharedInstance;
@implementation Database
- (id)init {
//Initiation Database, everything works fine
return self;
}
+ (Database *)sharedDatabase {
NSLog(@"picking shared Database");
return sharedInstance;
}
- (NSMutableArray *)query:(NSString *)stmt {
[result removeAllObjects];
if (!statement) {
const char *cQuery = [stmt cStringUsingEncoding:NSUTF8StringEncoding];
if (sqlite3_prepare_v2(database, cQuery, -1, &statement, NULL) != SQLITE_OK) {
NSLog(@"query error: %s", statement);
} else {
NSLog(@"Statement OK!");
}
}
while (sqlite3_step(statement) == SQLITE_ROW) {
const char *cStateName = (const char *)sqlite3_column_text(statement, 1);
NSString *stateName = [[[NSString alloc] initWithUTF8String:cStateName] autorelease];
NSInteger iStateId = sqlite3_column_int(statement, 0);
NSNumber *nStateId = [NSNumber numberWithInt:iStateId];
NSDictionary *stateDict = [[NSDictionary alloc] initWithObjectsAndKeys:nStateId,@"id",
stateName,@"name", nil];
[result addObject:stateDict];
[stateDict release];
NSLog(@"%@, %@", nStateId, stateName);
}
sqlite3_reset(statement);
return result;
}
@end
My TableView.h
Code:
#import <UIKit/UIKit.h>
@interface StateTableViewController : UIViewController <UITableViewDelegate> {
IBOutlet UITableView *stateTable;
NSMutableArray *states;
}
@end
TableView.m
Code:
#import "StateTableViewController.h"
#import "Database.h";
@implementation StateTableViewController
#pragma mark -
#pragma mark init
- (id)init {
//...
states = [[NSMutableArray alloc] initWithArray:[[Database sharedDatabase] query:@"SELECT id, name FROM States ORDER BY name"]];
return self;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [states count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)ip {
NSDictionary *stateDict = [states objectAtIndex:[ip row]];
NSString *name = [stateDict objectForKey:@"name"];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:@"UITableViewCell"];
[cell autorelease];
}
[[cell textLabel] setText:name];
return cell;
}
@end
And please feel free to correct every other mistake I made, or things you usually do different when you are programming for a iphone app. I'm quite new to this topic. Thanks!!