从iphone app中的sqlite db读取相同的记录

时间:2012-03-18 10:28:21

标签: ios5 sqlite xcode4.2

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

在AppDelegate中:

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

1 个答案:

答案 0 :(得分:0)

问题在于,无论何时选择图标,都会调用readSalesFromDatabase方法,每次都会将数据附加到“deals”数组中。因此,避免相同数据的冗余,确保在调用readSalesFromDatabase之前从交易数组中删除所有对象以继续进行,即      [deals removeallobjects]; 希望这会奏效。