Выход из приложения iPhone при прокрутке UITableView

Я объявил NSMutableArray и заполнил его информацией из базы данных. В таблице хорошо отображается информация. Но когда я прокручиваю вниз, приложение закрывается. Похоже, он теряет указатель на массив.

Вот код объявления массива:

@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;

Здесь я инициализирую массив объектом типа 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));
 }

}

вот конструктор для объекта StoreInfo

-(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;
}

Вот код для отображения ячейки:

- (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;
}

Сначала таблица отображается нормально. Но когда я прокручиваю вниз, это даже срабатывает и почему-то не может найти ссылку на si.storeName.

Я потратил часы, пытаясь отладить проблему. Любая помощь приветствуется.


person Shirish    schedule 30.01.2010    source источник


Ответы (1)


Прежде всего, как вы определили свойство проблемного поля? Это retain?

Во-вторых, можете ли вы получить доступ к любому другому свойству в si?

И, наконец, я вижу, что в self.storeList = [[NSMutableArray alloc] init]; есть утечка памяти - объект сохраняется дважды (в init и в установщике свойств)...

person Michael Kessler    schedule 30.01.2010
comment
Поле свойства не было определено как сохранение. После определения как сохранения это устранило проблему. СПАСИБО СПАСИБО СПАСИБО!! - person Shirish; 31.01.2010