简体   繁体   中英

iPhone Application Exit when scrolling the UITableView

I have declared a NSMutableArray and I populated it with information from the database. The table displays information well. But when I scroll down, the application exits. Seems like its loosing the pointer to the array.

Here is the code for declaration of the array:

@interface RootViewController : UITableViewController <CLLocationManagerDelegate> {
 sqlite3 *database;

 NSMutableArray *storeList;
 CLLocationManager *locationManager;
 CLLocation *startingPoint;

}

@property (nonatomic, retain) NSMutableArray *storeList;
@property (nonatomic, retain) CLLocationManager *locationManager;
@property (nonatomic, retain) CLLocation *startingPoint;    

- (void) createCopyOfDatabaseIfNeeded;
- (void) initializeStoreList;
- (void) getDistanceFromUserLocation;

Here I am initializing the array with object of type StoreInfo:

- (void) initializeStoreList{
 self.storeList = [[NSMutableArray alloc] init];

 //database is stored in the application bundle.
 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
 NSString *documentsDirectory = [paths objectAtIndex:0];
 NSString *dbPath = [documentsDirectory stringByAppendingPathComponent:kFileName];

 if (sqlite3_open([dbPath UTF8String], &database)== SQLITE_OK) {
  const char *sql = "select id, storename, ratings, lattitude, longitude from storeinformation";
  sqlite3_stmt *statement;

  if (sqlite3_prepare_v2(database, sql, -1, &statement, NULL) == SQLITE_OK) {
   while (sqlite3_step(statement) == SQLITE_ROW) {
    NSInteger *_pk = (NSInteger *) sqlite3_column_int(statement, 0);
    NSString *_storeName =  [NSString stringWithUTF8String:(char*) sqlite3_column_text(statement, 1)];
    NSString *_ratings = [NSString stringWithUTF8String:(char*) sqlite3_column_text(statement, 2)];
    double _lattitude = [[NSString stringWithUTF8String:(char*) sqlite3_column_text(statement, 3)] doubleValue];
    double _longitude = [[NSString stringWithUTF8String:(char*) sqlite3_column_text(statement, 4)] doubleValue];

    StoreInfo *si = [[StoreInfo alloc] initWithBasicInformation:_pk storeName:_storeName ratings:_ratings lattitude:_lattitude longitude:_longitude];
    [self.storeList addObject:si];
    [si release];

   }
  }

  sqlite3_finalize(statement);
 } else {
  sqlite3_close(database);
  NSAssert1(0,@"Failed to open the database with message '%s'.", sqlite3_errmsg(database));
 }

}

here is the constructor for StoreInfo object

-(id)initWithBasicInformation:(NSInteger *)_pk storeName:(NSString *) _storeName ratings:(NSString *) _ratings lattitude:(double) _lattitude longitude:(double) _longitude;
{
     if (self = [super init]) {
  self.primaryKey = _pk;
  self.storeName = _storeName;
  self.ratings = _ratings;
  self.lattitude = _lattitude;
  self.longitude = _longitude;
 }
 return self;
}

Here is the code for displaying the cell:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }

 // Configure the cell.
 StoreInfo *si = (StoreInfo *)[self.storeList objectAtIndex:indexPath.row];
 cell.textLabel.text = si.storeName;
    return cell;
}

The table displays alright first. But when I scroll down, this even gets fired and somehow it is not able to find reference to the si.storeName.

I have spent hours trying to debug the issue. Any help is greatly appreciated.

First of all, how have you defined the property for the problematic field? Is it retain ?

Secondly, Can you access any other property in si ?

And finally, I see that there is a memory leak in self.storeList = [[NSMutableArray alloc] init]; - the object is retained twice (in init and in the property setter)...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM