added object not updated in UITableView using SQlite database
I have a SQL database which I can display on a UITableView. But if try to add any object it is not updated in UITableView. It is updated only after application is restarted.
When I delete objects from UITableView there is inbuilt method which does deleting
is if (editingStyle == UITableViewCellEditingStyleDelete)
is there any such methods
for adding objects also?
My addition methods are as follows:
static sqlite3 *addStmt = nil;
- (void) add_Place:(Place *)PlaceObj {
NSLog(@"Super add called");
//Add it to the database.
[PlaceObj addPlace];
//Add it to the coffee array.
[PlacesArray addObject:PlaceObj];
}
- (void) addPlace {
NSLog(@"addplace called");
if(addStmt == nil)
{
NSLog(@"inside addstmt");
const char *sql = "INSERT INTO places_table ( PlaceName, PlaceAddress) Values (?,?)";
if(sqlite3_prepare_v2(data开发者_Python百科base, sql, -1, &addStmt, NULL) != SQLITE_OK)
{
NSLog(@"error inside if loop");
NSAssert1(0, @"Error while creating add statement. '%s'", sqlite3_errmsg(database));
}
}
NSLog(@"outside if Loop") ;
sqlite3_bind_text(addStmt, 1, [PlaceName UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(addStmt, 2, [PlaceAddress UTF8String], -1, SQLITE_TRANSIENT);
if(SQLITE_DONE != sqlite3_step(addStmt))
{
NSAssert1(0, @"Error while inserting data. '%s'", sqlite3_errmsg(database));;
}
// NSAssert1(0, @"Error while inserting data. '%s'", sqlite3_errmsg(database));
else
{
//SQLite provides a method to get the last primary key inserted by using sqlite3_last_insert_rowid
PlaceID = sqlite3_last_insert_rowid(database);
NSLog(@"sqlite3_last_insert_rowid") ;
}
//Reset the add statement.
sqlite3_reset(addStmt);
}
You can do this in the following manner. After you add the object just call the ReloadData
UITableView Method and it will reload the table again. So there is no need To restart the application again.
static sqlite3 *addStmt = nil;
- (void) add_Place:(Place *)PlaceObj {
NSLog(@"Super add called");
//Add it to the database.
[PlaceObj addPlace];
//Add it to the coffee array.
[PlacesArray addObject:PlaceObj];
[TableView ReloadData];
}
- (void) addPlace {
NSLog(@"addplace called");
if(addStmt == nil)
{
NSLog(@"inside addstmt");
const char *sql = "INSERT INTO places_table ( PlaceName, PlaceAddress) Values (?,?)";
if(sqlite3_prepare_v2(database, sql, -1, &addStmt, NULL) != SQLITE_OK)
{
NSLog(@"error inside if loop");
NSAssert1(0, @"Error while creating add statement. '%s'", sqlite3_errmsg(database));
}
}
NSLog(@"outside if Loop") ;
sqlite3_bind_text(addStmt, 1, [PlaceName UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(addStmt, 2, [PlaceAddress UTF8String], -1, SQLITE_TRANSIENT);
if(SQLITE_DONE != sqlite3_step(addStmt))
{
NSAssert1(0, @"Error while inserting data. '%s'", sqlite3_errmsg(database));;
}
// NSAssert1(0, @"Error while inserting data. '%s'", sqlite3_errmsg(database));
else
{
//SQLite provides a method to get the last primary key inserted by using sqlite3_last_insert_rowid
PlaceID = sqlite3_last_insert_rowid(database);
NSLog(@"sqlite3_last_insert_rowid") ;
}
//Reset the add statement.
sqlite3_reset(addStmt);
}
Cheers
I think what you are looking for is insertRowsAtIndexPaths:withRowAnimation
. This will add the rows with a smooth animation of your choosing. This will only work if your UITableViewDataSource
is coded properly however.
精彩评论