Ios5 在iphone应用程序中从sqlite db读取相同的记录

Ios5 在iphone应用程序中从sqlite db读取相同的记录,ios5,sqlite,xcode4.2,Ios5,Sqlite,Xcode4.2,在我的iphone应用程序中,我使用sqlite db。我有一个图标,从数据库开始读取并将其存储在表视图中。问题是:如果我再次选择图标,它会使具有相同记录的表视图加倍。我知道原因,因为每次我选择图标时,程序都会转到“readSalesFromDatabase”和“tableView cellForRowAtIndexPath”。问题是如何避免这种情况? 这是我的密码: 在AppDelegate中: } } 在控制器中: } 问题在于,每当您选择图标时,就会调用readSalesFromDatab

在我的iphone应用程序中,我使用sqlite db。我有一个图标,从数据库开始读取并将其存储在表视图中。问题是:如果我再次选择图标,它会使具有相同记录的表视图加倍。我知道原因,因为每次我选择图标时,程序都会转到“readSalesFromDatabase”和“tableView cellForRowAtIndexPath”。问题是如何避免这种情况?
这是我的密码:

在AppDelegate中: }

}

在控制器中: }


问题在于,每当您选择图标时,就会调用
readSalesFromDatabase
方法,该方法每次都将数据追加到“
deals
”数组中。因此,要避免相同数据的冗余,请确保在调用
readSalesFromDatabase
继续之前从deals数组中删除所有对象。 [
交易
删除所有对象
]; 希望这能奏效

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:   (NSDictionary *)launchOptions
{
// db name
databaseName = @"saSh5.sqlite";    
//creating db
[self checkAndCreateDatabase];
//deals will hold the retrive data
deals = [[NSMutableArray alloc] init]; 
return YES;
-(void) readSalesFromDatabase {

if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {
    // Setup the SQL Statement and compile it for faster access
    // const char *sqlStatement = "select * from UsersSale";
    const char *sqlStatement = "select us.userID , us.saleStoreID,  from UsersSale us  order by us.saleID";        


    sqlite3_stmt *compiledStatement;
    if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) {
        // Loop through the results and add them to the feeds array
         while(sqlite3_step(compiledStatement) == SQLITE_ROW) {
            // Read the data from the result row
            NSInteger auserID = sqlite3_column_int(compiledStatement, 0); 
            NSInteger asalStoreID = sqlite3_column_int(compiledStatement, 1); 

            // Create a new  Sale object with the data from the database                
            Deals *sfl  = [[Deals alloc] initWithName:auserID 
                                                            saleStoreID:asalStoreID]; 

            [deals addObject:sfl];
            [sfl release];

        }
}
    // Release the compiled statement from memory
    sqlite3_finalize(compiledStatement);

}
sqlite3_close(database);
 - (void)viewDidLoad
{
  [super viewDidLoad];
  // Do any additional setup after loading the view from its nib.
  self.view.backgroundColor = [UIColor whiteColor];
  self.title = @"Deals";
  self.myTableView = [[UITableView alloc] initWithFrame:self.view.bounds  style:UITableViewStylePlain];
  self.myTableView.dataSource = self; 
  self.myTableView.delegate = self;
  [self.view addSubview:self.myTableView]; 

  AppDelegate *appDelegate = ( AppDelegate *)[[UIApplication sharedApplication] delegate];
  [appDelegate readSalesFromDatabase];  
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

UILabel *product, *name; 
UITableViewCell *result = nil;
if ([tableView isEqual:self.myTableView]){
    static NSString *TableViewCellIdentifier = @"MyCells";
    result = [tableView dequeueReusableCellWithIdentifier:TableViewCellIdentifier];
    if (result == nil){
        result = [[UITableViewCell alloc]
                  initWithStyle: UITableViewCellStyleSubtitle reuseIdentifier:TableViewCellIdentifier];

    result = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
                                   reuseIdentifier:TableViewCellIdentifier] autorelease];
     AppDelegate *appDelegate = ( AppDelegate *)[[UIApplication sharedApplication] delegate];
    Deals *dls = (Deals *)[appDelegate.deals objectAtIndex:indexPath.row];        
    result.textLabel.lineBreakMode= UILineBreakModeWordWrap;

    product = [[[UILabel alloc] initWithFrame:CGRectMake(25.0, 0.0, 220.0, 15.0)] autorelease];
    product.tag = MAINLABEL_TAG;
    product.font = [UIFont systemFontOfSize:14.0];
    product.textAlignment = UITextAlignmentLeft;
    product.textColor = [UIColor blackColor];
    product.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleHeight;
    product.text = dls.saleSpecificProduct;        

    [result.contentView addSubview:product];

}   

return result; 
}