How to pass variable from uitableview to second tableview using didSelectRowAtIndexPa

Joined
Aug 9, 2012
Messages
4
Reaction score
0
Points
1
Hy all, using sqlite3 with tableviews, the fact of passing data from a tableView to a view controller did not actually work.( Tried the prepare of segue and did not work ) So instead i was trying to assign the content of the selected row into a global variable accessed then by the 2nd view controller. how can i do it?
 
OP
E
Joined
Aug 9, 2012
Messages
4
Reaction score
0
Points
1
i want to pass the variable from a view controller to another one, so that when the user selects a certain row in the first table view, the application will take him to another view controller in which the details of the selected item will appear.
This is my code :
Code:
Code:
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
        
    	NSString *selectedAuthors = [theauthors objectAtIndex:indexPath.row];
    	
        
        UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
        Details *dvController = [storyboard instantiateViewControllerWithIdentifier:@"Details"]; //Or whatever identifier you have defined in your storyboard
        
    
        
    	
           
        dvController.selectedAuthors = selectedAuthors;

        UIAlertView *messageAlert = [[UIAlertView alloc]
                                    initWithTitle:@"Row Selected" message:authorNAme delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
       
       // Display Alert Message
        authorNAme =  [[NSString stringWithFormat:[theauthors objectAtIndex:indexPath.row]] intValue];
     [messageAlert show];
        
    	[self.navigationController pushViewController:dvController animated:YES];
    }
selectedAuthors is a string
authorName is a global variable in which i want to store the content of the selected row.
Any help will be highly appreciated.
 
OP
E
Joined
Aug 9, 2012
Messages
4
Reaction score
0
Points
1
The answer

Code:
     NSString *titleString = [[[NSString alloc] initWithFormat:@"Element number :  %d",indexPath.row] autorelease];
This is the simplest function used to send variables and it just came out of nowhere! For anyone lost this is the best function!!
 
OP
E
Joined
Aug 9, 2012
Messages
4
Reaction score
0
Points
1
SQLite : Perform query with a variable parameter

Hy all , I have an application that uses two view controllers. Recently i have managed to pass data between the two views, in fact when a row is selected , it will automatically send to the other view controller his proper id. Now i need that a query will be executed automatically when the row is selected in the following matter :

Code:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    //Get the selected country
    NSString *selectedAuthors = [theauthors objectAtIndex:indexPath.row];
    //NSLog(selectedAuthors);

    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
    Details *dvController = [storyboard instantiateViewControllerWithIdentifier:@"Details"]; //Or whatever identifier you have defined in your storyboard

    //authorNAme = selectedAuthors.description;

     NSString *titleString = [[[NSString alloc] initWithFormat:@"Element number :  %d",indexPath.row] autorelease];

    authorNAme = titleString;
    //Initialize the detail view controller and display it.
    //Details *dvController = [[Details alloc] init/*WithNibName:@"Details" bundle:nil*/];

    dvController.selectedAuthors = selectedAuthors;

    @try {

        NSFileManager *fileMgr2 = [NSFileManager defaultManager];

        // NSString *dbPath = [[[NSBundle mainBundle] resourcePath ]stringByAppendingPathComponent:@"dictionary.sqlite"];
        //NSString *dbPath = [[[NSBundle mainBundle] resourcePath ]stringByAppendingPathComponent:@"authorsDb2.sqlite"];
        //     NSString *dbPath = [[[NSBundle mainBundle] resourcePath ]stringByAppendingPathComponent:@"FinalDb.sqlite"];
        //NSString *dbPath = [[[NSBundle mainBundle] resourcePath ]stringByAppendingPathComponent:@"xxJuridique-FINAL-OK.sqlite"];

        NSString *dbPath2 = [[[NSBundle mainBundle] resourcePath ]stringByAppendingPathComponent:@"data.sqlite"];

        BOOL success = [fileMgr2 fileExistsAtPath:dbPath2];
        if(!success)
        {
            NSLog(@"Cannot locate database file '%@'.", dbPath2);
        }
        if(!(sqlite3_open([dbPath2 UTF8String], &db) == SQLITE_OK))
        {
            NSLog(@"An error has occured: %@", sqlite3_errmsg(db));

        }
        NSLog(@"access to the second DB is ok");


        // const char *sql = "SELECT F_Keyword FROM wordss";  
        const char *sql2 = "SELECT field7 FROM Sheet1 WHERE field1 = 'titleString' ";
        //NSLog(sql2);

        sqlite3_stmt *sqlStatement2;
        if(sqlite3_prepare(db, sql2, -1, &sqlStatement2, NULL) != SQLITE_OK)
        {   NSLog(@"Problem with prepare the db");
            NSLog(@"Problem with prepare statement:  %@", sqlite3_errmsg(db));
        }else{

  //  while (sqlite3_step(sqlStatement2)==SQLITE_ACCESS_EXISTS) {

            NSLog(@"Starting to prepare the result");
            Author * author2 = [[Author alloc] init];
            NSLog(@"Author 2 created");

            author2.genre2 = [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement2, 7 )];
            NSLog(@"Initialistion of author 2 is ok");

        //        NSLog(author2.genre);
              // authorNAme = author2.genre;   


            [theauthors addObject:author2];

   // }

        }

    }
    @catch (NSException *exception) {
        NSLog(@"Problem with prepare statement:  %@", sqlite3_errmsg(db));
    }
    @finally {
        //   sqlite3_finalize(sqlStatement);.
        //   authorNAme = nil;
        sqlite3_close(db);
        //   authorNAme = Nil;
        return theauthors;
    }



    UIAlertView *messageAlert = [[UIAlertView alloc]
                                initWithTitle:@"Row Selected" message:titleString delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];

 [messageAlert show];

    [self.navigationController pushViewController:dvController animated:YES];

}
The variable containing the id is titleString, and i need when the row is selected, to select the field7 of my table where the field1 is equal to titleString. Any Ideas?
 

RavingMac

Well-known member
Staff member
Moderator
Joined
Jan 7, 2008
Messages
8,303
Reaction score
242
Points
63
Location
In Denial
Your Mac's Specs
16Gb Mac Mini 2018, 15" MacBook Pro 2012 1 TB SSD
Threads merged and moved.

Please use a single thread for related posts. And, for better help on iOS Development issues, please post in the iOS Development Forum.

Thanks,
Razormac
 

Shop Amazon


Shop for your Apple, Mac, iPhone and other computer products on Amazon.
We are a participant in the Amazon Services LLC Associates Program, an affiliate program designed to provide a means for us to earn fees by linking to Amazon and affiliated sites.
Top