NSTableView?

Dogcow

Registered
A small (but essential) part of a program I will soon start coding has me stumped... I'll be creating a 2-d array of integers such that an element is less than the element below it and beside it. (Basically this will be a table of random numbers that are just in ascending order, first to the right, then starting at the front of the next line.) I need a data storage type that is compatible with this method of storage.

Secondly, I need a way to neatly display said array in a tabular manner... by row and column as they're stored. The table is for a searching algorithm, so neatness is essential...

I know that NSTableView likes NSMutableArrays, and I could add NSNumbers to that array, but is it possible to add them in the manner that I need to?

So basically I'm asking, what's the best approach to this? If there is no perfect solution, what are other possibilities to get this working?

Thanks in advance,
-Dogcow "moof!"
 
It's difficult to offer a suggestion without knowing the details of what your program will do. I am still learning how to program Cocoa/Obj-C, but one solution that occurs to me is to use an NSMutableArray of NSMutableDictionary objects: an NSMutableDictionary for each row, and each dictionary entry's key value set to the appropriate NSTableView column identifier. You would apply algorithms to the data, then reload the tableview data to reflect changes.

This is a very rough bit of code I've hacked together to demonstrate what I'm talking about. You could subclass the array to a separate model class to tidy things up (there are almost certainly more elegant solutions).

A controller class awakeFromNib method:
Code:
- (void)awakeFromNib
{
    // the dimensions of your array
    int columns = 10;
    int rows = 10;

    int i,j;
    
    // whatever code and data you're using for your array's elements
    int arrayValue = 100;

    // Your NSTableView's column identifiers
    NSArray *identifiers = [NSArray arrayWithObjects: @"1", @"2",@"3",@"4",@"5",
                                                 @"6",@"7",@"8",@"9",@"10", nil];

    //instance variables stores each row's data
    tableViewArray = [NSMutableArray arrayWithCapacity:rows];
    
    //step through the rows
    for (i = 0; i < rows; i++) {
        // local dictionary to store a row's values and accessor keys for values
        NSMutableDictionary *tempDict = [NSMutableDictionary
                                        dictionaryWithCapacity:columns];
                                        
        //step through each column of a row, adding each value with its appropriate key
        for (j = columns -1; j >= 0; j--) {
            //whatever code generates your elements
            NSNumber *temp = [NSNumber numberWithInt: arrayValue];
            arrayValue--;
            [tempDict setObject: temp forKey:[identifiers objectAtIndex:j]];
        }
        
        //insert the dictionary into the array of rows
        [tableViewArray insertObject: tempDict atIndex:i];
    }
    [tableViewArray retain];
}
and tableview delegate methods:
Code:
- (int)numberOfRowsInTableView:(NSTableView *)aTableView
{
    return [tableViewArray count];
}

- (id)tableView:(NSTableView *)aTableView
        objectValueForTableColumn:(NSTableColumn *)aTableColumn
        row:(int)rowIndex
{
    NSString *identifier = [aTableColumn identifier];
    NSMutableDictionary *d = [[NSMutableDictionary alloc] init];
   
    //read this row's data from the array of dictionaries
    [d setDictionary: [tableViewArray objectAtIndex:rowIndex]];
    
    //return the element at key corresponding to the current column's identifier
    return [d objectForKey: identifier];
}
 
You could try making a tree structure for your number data, then just have the different cells in the table display them. Though this would be easier to implement if you know how many numbers are being displayed - you didn't say.
 
Yeah, I won't know ahead of time what the size of the array will be. It will be chosen by the user at runtime.

Basically, my program will first create an array of random numbers such that each number is great than the one previous to it. I already have this code covered. This array will be searched via two searching methods.... a naive approach and an approach that I come up with. Both of these are in pseudo-code stages. I could easily put the info in a 2D integer array, but then comes the problem of how to neatly output them to some sort of field.

I hope this clears it up somewhat... As soon as I get out of this class and back to my Mac I'll post a screenshot of the program's GUI.

Thanks in advance,
-Dogcow "moof!"
 
The GUI:

searchRoutines.jpg
 
Back
Top