Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 08-09-2010, 02:41 AM   #1 (permalink)
Registered Member
 
Join Date: Aug 2010
Posts: 3
ZeRo is on a distinguished road
Exclamation TableView crashes when scrolling, getting the Data from SQLite.

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!!
ZeRo is offline   Reply With Quote
Reply

Bookmarks

Tags
sqlite, tableview, tableview add cell, tableviewcell

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 337
10 members and 327 guests
arash5500, HemiMG, iram91419, linkmx, mediaspree, Objective Zero, Paul Slocum, stanny, Touchmint, v1n2e7t
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,656
Threads: 94,116
Posts: 402,889
Top Poster: BrianSlick (7,990)
Welcome to our newest member, iram91419
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 11:36 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0