2010-01-30 66 views
1

我已經聲明瞭一個NSMutableArray,並使用來自數據庫的信息填充它。該表很好地顯示信息。但是當我向下滾動時,應用程序退出。好像它丟失了指向數組的指針。iPhone應用程序在滾動UITableView時退出

下面是對數組的聲明代碼:

@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的引用。

我花了數小時試圖調試問題。任何幫助是極大的讚賞。

回答

0

首先,你是如何定義問題領域的財產? 是不是retain

其次,您是否可以訪問si中的任何其他房產?

最後,我看到有內存泄漏在self.storeList = [[NSMutableArray alloc] init]; - 對象被保持兩次(在init和屬性setter)...

+0

的屬性字段沒有被定義爲保留。定義爲保留後,它解決了問題。謝謝你,謝謝你,謝謝你!! – Shirish 2010-01-30 22:07:46