Hi! This is my first post here, really hoping someone can help as I have searched for ages trying to solve this (I am a relative noob to Obj-C), anyway heres the situation...
I have an application with two table views:
The first view has an array of 'todo' items. Each item/row has a button, when clicked it updates the sqlite db, marking an item as complete (status =1);
The second view has an array of these completed 'todo' items;
Now this is working except that in the first tableview I can't seem to bind the cell indexpath.row to the primarykey for the item in that cell when the button is clicked (indexpath starts at 0, primarykey starts at 1).
Any advice is greatly appreciated... I can post some code if that would help?
Many thanks!
Code:
RootViewController.m
Code:
// called when a user presses the button to alter the status
- (void)updateStatus:(id)sender {
NSIndexPath *indexPath = [self.tableView indexPathForCell:(UITableViewCell *)[sender superview]];
NSInteger row = indexPath.row;
NSLog(@"The row is %i", row);
todoAppDelegate *appDelegate = (todoAppDelegate *)[[UIApplication sharedApplication] delegate];
Todo *td = [appDelegate.todos objectAtIndex:indexPath.row];
if (td.status == 0) {
[td updateStatus:1];
NSLog(@"Status is %i",td.status);
}
else {
[td updateStatus:0];
NSLog(@"Status is %i",td.status);
}
}
todoAppDelegate.m
Code:
-(void)updateItemAtIndexPath:(NSIndexPath *)path {
Todo *td = (Todo *)[todos objectAtIndex:path.row];
int ret;
const char *sql = "update todo set complete = ? where pk= ?;";
if (dehydrate_statment == nil)
{ // build update statement
if ((ret=sqlite3_prepare_v2(database, sql, -1, &dehydrate_statment, NULL))!=SQLITE_OK)
{
NSAssert1(0, @"Error building statement to update todos [%s]", sqlite3_errmsg(database));
}
}
// bind values to statement
NSInteger p = td.primaryKey;
NSLog(@"tdprimaryKey is %i",td.primaryKey);
sqlite3_bind_int(dehydrate_statment, 2, p);
NSInteger s = td.status;
sqlite3_bind_int(dehydrate_statment, 1, s);
// now execute sql statement
if ((ret=sqlite3_step(dehydrate_statment)) != SQLITE_DONE)
{
NSAssert1(0, @"Error updating values [%s]", sqlite3_errmsg(database));
}
// now reset bound statement to original state
sqlite3_reset(dehydrate_statment);
NSLog(@"updateItemAtIndexPath called");
}
Problem here is I cant get the indexPath.row to match the primaryKey and I can't pass the indexPath.row to the todoAppDelegate...