Converting a CLI to GUI application. NSTableview

Discussion in 'Mac Programming' started by Cabbit, Aug 25, 2010.

  1. Cabbit macrumors 68020

    Cabbit

    Joined:
    Jan 30, 2006
    Location:
    Scotland
    #1
    I have a CLI application that can read and write to a mysql database. I would now like to start moving this to GUI based app and the first thing i want to do is show the results of a database table to a NSTableView.

    So i started by creating a new Cocoa application without any options clicked and i moved over the class.

    I then edited my AdminAppDelegate.m file with the following code.
    Code:
    #import "AB_Comforts_AdminAppDelegate.h"
    #import "MysqlConnection.h"
    #import "MysqlCommitException.h"
    #import "MysqlRollbackException.h"
    #import "GC_MYSQL_BIND.h"
    #import "MysqlFetch.h"
    #import "MysqlFetchField.h"
    
    @implementation AB_Comforts_AdminAppDelegate
    
    @synthesize window;
    
    - (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
    	// Connect to the database
        MysqlConnection *connection = [MysqlConnection connectToHost:@"localhost"
    															user:@"root"
    														password:@""
    														  schema:@"test_db"
    														   flags:MYSQL_DEFAULT_CONNECTION_FLAGS];
    	
    	// Fetch the users from the database
    	MysqlFetch *userFetch = [MysqlFetch fetchWithCommand:@"select username, email from users" onConnection:connection];
    	
    	// Print out a list of users
    	NSLog(@"There are %d users",[userFetch.results count]);
    	for (NSDictionary *userRow in userFetch.results) {
    		NSNumber *userNumber = [userRow objectForKey: @"username"];
    		NSString *userName = [userRow objectForKey: @"email"];
    		NSLog(@"%@ %@",userNumber,userName);
    	}
    }
    
    @end
    And here is were i got stuck, my only .xib is MainMenu it has a menu and a window so i put in a NSTableView and tried the trusty ctrl click to match it up with the controller as i have done with some other applications(built with the core data and document based selected) however this did not work.

    Any idea where about i am going wrong, my aim for now is just to get my data on the gui instead of the console log.
     
  2. ytk macrumors regular

    Joined:
    Jul 8, 2010
    #2
    If you are just trying to display the data in a GUI field rather than a console, then NSTableView is the wrong class to use for this. You might want to use an NSTextField instead. You would then have to append the output to the NSTextField rather than passing it to NSLog.

    If you really do want to use a table view for some reason, you need to create an object that conforms to the NSTableViewDataSource protocol. One way to do this is to create a new class following this protocol, with an additional method to receive the data and organize it. Then instantiate this class in applicationDidFinishLaunching: and call setDataSource: on the NSTableView, passing the object. Any time you add data to the data source object you will need to call reloadData on the NSTableView, though.
     
  3. Catfish_Man macrumors 68030

    Catfish_Man

    Joined:
    Sep 13, 2001
    Location:
    Portland, OR
    #3
    NSTextField is definitely not the right choice for log-style data. NSTextView would be, perhaps.
     
  4. Cabbit thread starter macrumors 68020

    Cabbit

    Joined:
    Jan 30, 2006
    Location:
    Scotland
    #4
    I should have explained better, the application is eventually going to be a admin for my Ecommerce and forum i have built with Ruby on Rails 3.0. I wish to make the admin a native Mac OS application and perhaps in the future a iPad application so i can manage my store better.

    I am going for a Cocoa application in part to learn Cocoa and Objective C, and also in part to use the CPU for some heavy lifting tasks like compressing a video before upload.

    Now with this in mind i think the best way to display database tables is to put them into a table so this is why i went for NSTableView.
     

Share This Page