I am pretty noob to iOS dev and Objective-C, I am making a brownie points app. I am using a sqlite DB that stores employee names and how many points they have, I also have a custom cell that displays the name and how many points they have on labels.
To increase the points they have I have a button on the cell, which points the SQL Update Statement needed for updating their points.
My issue is that when I click the button everything works but it doesnt display the updated points until I navigate away from the view and then back to it. I have tried :
[self.collectionview reloadData];
I have tried this in the button action and I have even tried adding a tap gesture recognizer so that whenever the user taps anywher it reloads the data. All with no success, I am not getting errors or crashes, it just doesnt refresh the labels.
Any Help will be hugely appreciated <3
Edit : If it helps, I have one class called DatabaseHelper that has all the methods to do with SQL in it .
This is my update method :
+(void)minusPoints:(NSString *)cell {
[self databaseInit];
if (sqlite3_open(dbpath, &peopleDB)== SQLITE_OK) {
NSString *updateSQL = [NSString stringWithFormat:@"UPDATE PEOPLE SET POINTS = (POINTS - 1) WHERE ID =%@", cell];
sqlite3_prepare_v2(peopleDB,[updateSQL UTF8String],-1,&statement,NULL);
if (sqlite3_step(statement)== SQLITE_DONE) {
NSLog(@"Updated Successfully");
}
else {
NSLog(@"%s SQLITE_ERROR '%s' (%1d)", __FUNCTION__, sqlite3_errmsg(peopleDB), sqlite3_errcode(peopleDB));
}
sqlite3_finalize(statement);
sqlite3_close(peopleDB);
}
}
Get data from DB :
+(NSMutableArray*)getData {
[self databaseInit];
peopleArray = [[NSMutableArray alloc] init];
if (sqlite3_open(dbpath, &peopleDB)== SQLITE_OK)
{
NSString *selectSQL = @"SELECT ID, NAME, POINTS FROM PEOPLE ORDER BY POINTS DESC";
sqlite3_prepare_v2(peopleDB,[selectSQL UTF8String],-1,&statement,NULL);
while (sqlite3_step(statement)== SQLITE_ROW)
{
PersonObject *newPerson = [[PersonObject alloc]init];
newPerson.ID = [[NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 0)]integerValue];
newPerson.name = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 1)];
newPerson.points = [[NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 2)] integerValue];
[peopleArray addObject:newPerson];
}
NSLog(@"%s SQLITE_ERROR '%s' (%1d)", __FUNCTION__, sqlite3_errmsg(peopleDB), sqlite3_errcode(peopleDB));
}
sqlite3_finalize(statement);
sqlite3_close(peopleDB);
return peopleArray;
}
Edit 2 : I have tried something like this in response to the first answer, still with no luck. (in ViewController.m )
//refresh cells
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [[event allTouches]anyObject];
if ([touch view]) {
dispatch_async(dispatch_get_main_queue(), ^{
[self.collectionView reloadData];
});
}
[super touchesBegan:touches withEvent:event];
}