In my iphone app I use a sqlite db. I have an icon that starts the reading from db and stores it in table view. The problem is: if I select the icon again, it doubles the table view with the same record. I know the reason, because every time I select the icon the program goes to ‘readSalesFromDatabase’ and to ‘tableView cellForRowAtIndexPath’. The problem is how to avoid this?
Here is my code:
In 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);
}
In Controller:
- (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;
}
The problem is that whenever you select the icon the
readSalesFromDatabasemethod gets called, which appends the data in ‘deals‘ array each time. So avoid the redundancy of the same data, make sure to remove all objects from deals array before callingreadSalesFromDatabaseto proceed further i.e.[
dealsremoveallobjects];Hope this will work.