Description: creating a data source for a data grid.
#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
#import "DataSource.h" #import <NuGridView/NuGridCell.h> #import <NuGridView/NuGridImageTextCell.h> #import "CustomCell.h" #import "SparklineCell.h" #import <NuGridView/NuGridCellWithSparkline.h> #import <NuGridView/NuGridExpandableCell.h> @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<5; i++) { NSString* rowStr = [NSString stringWithFormat:@"%d", i]; [rowHeader addObject:rowStr]; }; // Set column headers for (int i=0; i<5; i++) { NSString* columnStr = [NSString stringWithFormat:@"%c", 65 + i]; [columnHeader addObject:columnStr]; }; // Create table data NSMutableArray* temp; for (int i=0; i<5; i++) { temp = [NSMutableArray new]; for (int j=0; j<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:@"row_header"]; // 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:@"%@", 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:@"column_header"]; NSObject *value = [self gridView:gridView valueForHeaderForColumn:column number:number]; cell.value = value; cell.text = [NSString stringWithFormat:@"%@", 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:@"row"]; NSObject *value = [self gridView:gridView valueForCellInRow:row inColumn:column]; cell.value = value; cell.text = [NSString stringWithFormat:@"%@", 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 < [_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 < [_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:@"corner"]; 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 @"Grid"; } // Adds an observer to corresponding array of observers - (void)addObserver:(id<NuGridUpdateObserver>)observer { [observers addObject:observer]; } // Removes observer from corresponding array of observers - (void)removeObserver:(id<NuGridUpdateObserver>)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 >= 0; i--) { NSMutableArray* columnData = [_data objectAtIndex:i]; for (int j = ([columnData count] -1); j >= 0; j--) { if((j > [fromCell columnNumber]) & (j <= [toCell columnNumber])) { [columnData removeObjectAtIndex:j]; if (isColumnHeaderExpanded) { [columnHeader removeObjectAtIndex:j]; } } } if((i > [fromCell rowNumber]) & (i <= [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 < [_data count]; i++) { NSMutableArray* columnData = [_data objectAtIndex:i]; for (int j = 0; j < columnData.count; j++) { if((j > [fromCell columnNumber]) & (j <= [toCell columnNumber])) { [columnData insertObject:[NSNumber numberWithInt:(i+j)] atIndex:j]; if (isColumnHeaderCollapsed) { [columnHeader insertObject:[NSString stringWithFormat:@"%c", 65 + j] atIndex:j]; } } } if((i > [fromCell rowNumber]) & (i <= [toCell rowNumber])) { columnData = [[NSMutableArray new] autorelease]; for (int j = 0; j < columnHeader.count; j++) { [columnData addObject:[NSNumber numberWithInt:(i+j)]]; } [_data insertObject:columnData atIndex:i]; [rowHeader insertObject:[NSString stringWithFormat:@"%d", i] atIndex:i]; } isColumnHeaderCollapsed = false; } } @end
See also: