Data Source for a Simple Data Grid

Description: creating a data source for a data grid.

The DataSource.h File

#import <NuGridView/NuGridView.h>
#import <NuGridView/NuTextGridCell.h>
#import <NuGridView/NuGridDataSource.h>
@interface DataSource : NSObject<NuGridDataSource> {
    NSMutableArray *rowHeader; // Row headers
    NSMutableArray *columnHeader; // Column headers
    NSMutableArray *observers; // Array of observers
}
@property(assign) NSMutableArray *data; // Data array
- (id) init;
// Collapses specified table range
- (void)collapseRangeFromCell:(NuGridCell*)fromCell ToCell:(NuGridCell*) toCell;
// Expands specified table range
- (void)expandRangeFromCell:(NuGridCell*)fromCell ToCell:(NuGridCell*) toCell;
@end

The DataSource.m File

#import &quot;DataSource.h&quot;
#import &lt;NuGridView/NuGridCell.h&gt;
#import &lt;NuGridView/NuGridImageTextCell.h&gt;
#import &quot;CustomCell.h&quot;
#import &quot;SparklineCell.h&quot;
#import &lt;NuGridView/NuGridCellWithSparkline.h&gt;
#import &lt;NuGridView/NuGridExpandableCell.h&gt;
@implementation DataSource
@synthesize data = _data;
- (id) init {
    if (self=[super init])
    {
        // Initialize arrays
        observers = [NSMutableArray new];
        _data = [NSMutableArray new];
        rowHeader = [NSMutableArray new];
        columnHeader = [NSMutableArray new];
        // Set row headers
        for (int i=0; i&lt;5; i++) {
            NSString* rowStr = [NSString stringWithFormat:@&quot;%d&quot;, i];
            [rowHeader addObject:rowStr];
        };
        // Set column headers
        for (int i=0; i&lt;5; i++) {
            NSString* columnStr = [NSString stringWithFormat:@&quot;%c&quot;, 65 + i];
            [columnHeader addObject:columnStr];
        };
        // Create table data
        NSMutableArray* temp;
        for (int i=0; i&lt;5; i++) {
            temp = [NSMutableArray new];
            for (int j=0; j&lt;5; j++) {
                [temp addObject:[NSNumber numberWithInt:(i+j)]];
            }
            [_data addObject:temp];
            [temp release];
        }
        return self;
    }
    return nil;
}
// Returns cell in header row with setting of data
- (NuGridCell *)gridView:(NuGridView *)gridView headerForRow:(NSInteger)row number:(NSInteger)number {
    // Create a cell with identifier
    NuTextGridCell *cell = [[CustomCell alloc] initWithReuseIdentifier:@&quot;row_header&quot;];
    // Get cell value
    NSObject *value = [self gridView:gridView valueForHeaderForRow:row number:number];
    // Set cell value
    cell.value = value;
    // Set displayed cell value
    cell.text = [NSString stringWithFormat:@&quot;%@&quot;, value];
    // Specify row number
    cell.rowNumber = row;
    // Specify column number
    cell.columnNumber = number;
    // Set column cell index
    cell.columnIndex = 0;
    // Set row cell index
    cell.rowIndex = 0;
    // Specify that cell is column header
    cell.isColumnHeader = NO;
    // Specify that cell is not row header
    cell.isRowHeader = YES;
    // Link cell to table, in which it will be located
    cell.parentGrid = gridView;
    return cell;
}
// Returns cell by header column with setting of data
- (NuGridCell *)gridView:(NuGridView *)gridView headerForColumn:(NSInteger)column number:(NSInteger)number
{
    NuTextGridCell *cell = [[CustomCell alloc] initWithReuseIdentifier:@&quot;column_header&quot;];
    NSObject *value = [self gridView:gridView valueForHeaderForColumn:column number:number];
    cell.value = value;
    cell.text = [NSString stringWithFormat:@&quot;%@&quot;, value];
    cell.rowNumber = number;
    cell.columnNumber = column;
    cell.columnIndex = 0;
    cell.rowIndex = 0;
    cell.isColumnHeader = YES;
    cell.isRowHeader = NO;
    cell.parentGrid = gridView;
    return cell;
}
// Returns cell by column and row with setting of data
- (NuGridCell *)gridView:(NuGridView *)gridView cellInRow:(NSInteger)row inColumn:(NSInteger)column
{
    NuGridExpandableCell *cell = [[CustomCell alloc] initWithReuseIdentifier:@&quot;row&quot;];
    NSObject *value = [self gridView:gridView valueForCellInRow:row inColumn:column];
    cell.value = value;
    cell.text = [NSString stringWithFormat:@&quot;%@&quot;, value];
    cell.rowNumber = row;
    cell.columnNumber = column;
    cell.columnIndex = 0;
    cell.rowIndex = 0;
    cell.isColumnHeader = NO;
    cell.isRowHeader = NO;
    cell.parentGrid = gridView;
    return cell;
}
// Returns the number of table rows
- (NSInteger)gridViewRowCount:(NuGridView *)gridView
{
    return [_data count];
}
// Returns the number of table columns
- (NSInteger)gridViewColumnCount:(NuGridView *)gridView
{
    return [[_data objectAtIndex:0] count];
    
}
// Determine whether table contains header rows
- (BOOL)gridViewHasRowHeader:(NuGridView *)gridView
{
    return YES;
}
// Determine whether table contains header columns
- (BOOL)gridViewHasColumnHeader:(NuGridView *)gridView
{
    return YES;
}
// Returns the number of header rows 
-(NSInteger)gridViewRowHeaderCount:(NuGridView *)gridView
{
    return 1;
}
// Returns the number of header columns
-(NSInteger)gridViewColumnHeaderCount:(NuGridView *)gridView
{
    return 1;
}
// Returns the value stored in table row header
- (NSObject *)gridView:(NuGridView *)gridView valueForHeaderForRow:(NSInteger)row number:(NSInteger)number
{
    if (row &lt; [_data count]) {
        return [rowHeader objectAtIndex:row];
    }
    else
    return nil;
}
// Returns the value stored in table column header
- (NSObject *)gridView:(NuGridView *)gridView valueForHeaderForColumn:(NSInteger)column number:(NSInteger)number
{
    return [columnHeader objectAtIndex:column];
}
// Returns the value stored in table cell
- (NSObject *)gridView:(NuGridView *)gridView valueForCellInRow:(NSInteger)row inColumn:(NSInteger)column
{
    if (row &lt; [_data count]) {
        return [[_data objectAtIndex:row] objectAtIndex:column];
        
    }
    else
    return nil;
}
// Returns left top table cell
- (NuGridCell *) gridView:(NuGridView *) gridView cornerCellInRowNumber:(NSInteger)rowNumber inColumnNumber:(NSInteger)columnNumber
{
    SparklineCell *cell = [[SparklineCell alloc] initWithReuseIdentifier:@&quot;corner&quot;];
    cell.value = [self gridView:gridView valueForCornerCellInRowNumber:rowNumber inColumnNumber:columnNumber];
    cell.rowNumber = rowNumber;
    cell.columnNumber = columnNumber;
    cell.rowIndex = -1;
    cell.columnIndex = -1;
    cell.isRowHeader = YES;
    cell.isColumnHeader = YES;
    cell.parentGrid = gridView;
    return cell;
}
// Returns the value stored in the left top table cell
- (NSObject *)gridView:(NuGridView *)gridView valueForCornerCellInRowNumber:(NSInteger)rowNumber inColumnNumber:(NSInteger)columnNumber {
    return @&quot;Grid&quot;;
}
// Adds an observer to corresponding  array of observers
- (void)addObserver:(id&lt;NuGridUpdateObserver&gt;)observer
{
    [observers addObject:observer];
}
// Removes observer from corresponding array of observers
- (void)removeObserver:(id&lt;NuGridUpdateObserver&gt;)observer
{
    [observers removeObject:observer];
}
// Collapses specified range in table
-(void)collapseRangeFromCell:(NuGridCell*)fromCell ToCell:(NuGridCell*) toCell {
    bool isColumnHeaderExpanded = true;
    for (int i = ([_data count] - 1); i &gt;= 0; i--) {
        NSMutableArray* columnData = [_data objectAtIndex:i];
        for (int j = ([columnData count] -1); j &gt;= 0; j--) {
            if((j &gt; [fromCell columnNumber]) &amp; (j &lt;= [toCell columnNumber])) {
                [columnData removeObjectAtIndex:j];
                if (isColumnHeaderExpanded) {
                    [columnHeader removeObjectAtIndex:j];
                }
            }
        }
        if((i &gt; [fromCell rowNumber]) &amp; (i &lt;= [toCell rowNumber])) {
            [_data removeObjectAtIndex:i];
            [rowHeader removeObjectAtIndex:i];
        }
        isColumnHeaderExpanded = false;
    }
}
// Expands specified range in table
-(void)expandRangeFromCell:(NuGridCell*)fromCell ToCell:(NuGridCell*) toCell {
    bool isColumnHeaderCollapsed = true;
    for (int i = 0; i &lt; [_data count]; i++) {
        NSMutableArray* columnData = [_data objectAtIndex:i];
        for (int j = 0; j &lt; columnData.count; j++) {
            if((j &gt; [fromCell columnNumber]) &amp; (j &lt;= [toCell columnNumber])) {
                [columnData insertObject:[NSNumber numberWithInt:(i+j)] atIndex:j];
                if (isColumnHeaderCollapsed) {
                    [columnHeader insertObject:[NSString stringWithFormat:@&quot;%c&quot;, 65 + j] atIndex:j];
                }
            }
        }
        if((i &gt; [fromCell rowNumber]) &amp; (i &lt;= [toCell rowNumber])) {
            columnData = [[NSMutableArray new] autorelease];
            for (int j = 0; j &lt; columnHeader.count; j++) {
                [columnData addObject:[NSNumber numberWithInt:(i+j)]];
            }
            [_data insertObject:columnData atIndex:i];
            [rowHeader insertObject:[NSString stringWithFormat:@&quot;%d&quot;, i] atIndex:i];
        }
        isColumnHeaderCollapsed = false;
    }
}
@end

See also:

Creating a Simple Data Grid