Data Grid

Description: creating a data grid using specified data source and proxy data source.

The ViewController.h File

#import "ProxyDataSource.h"
#import "DataSource.h"
#import "NuMenu.h"
#import "LinearGradientBrush.h"
#import "RadialGradientBrush.h"
#import "GradientStop.h"
@interface ViewController : UIViewController<NuGridDelegate, UIPopoverControllerDelegate, NuMenuDelegate, NuGridActionDelegate> {
    DataSource *datasource; // Data source
    ProxyDataSource *proxyDatasource; // Proxy data source
    NuGridController *contr; // Table manager
    NSInteger colWidth; // Table column width
    NSInteger rowHeight; // Table row width
    
    UIPopoverController *popover; // Menu popup window
}
// Executes custom example placed in the body of this method
- (void) executeExample;
@end

The ViewController.m File

#import "ViewController.h"
#import <NuGridView/NuGridController.h>
#import <NuGridView/NuTextGridCell.h>
#import <NuGridView/NuGridGradientBrush.h>
#import <NuGridView/NuGridLinearGradientBrush.h>
#import <NuGridView/NuGridRadialGradientBrush.h>
#import <NuGridView/NuGridSolidColorBrush.h>
#import <NuGridView/NuGridCellImage.h>
#import <NuGridView/NuGridImageTextCell.h>
#import <NuGridView/NuGridExpandableCell.h>
#import "NuMenuController.h"
#import "SparklineCell.h"
#import "CustomCell.h"
@implementation ViewController
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Create a data source
    datasource = [[DataSource alloc] init];
    // Create a proxy data source
    proxyDatasource = [[ProxyDataSource alloc] init];
    // Set link to data source for proxy data source
    [proxyDatasource setDataSource:datasource];
    // Create an object controlling table
    contr = [[NuGridController alloc] init];
    // Set link to proxy data source
    [contr setDataSource:proxyDatasource];
    // Set object for controlling table basic functionality
    [contr setDelegate:self];
    // Set object for executing various operations on table
    [contr setActionDelegate:self];
    // Enable sorting
    [contr setCanSort:YES];
    // Enable using of fixed rows and columns
    [contr setCanFix:YES];
    [contr setDockable:YES];
    // Determine color and thickness of horizontal separator line
    [contr setHorizontalSeparateLineColor:[UIColor redColor]];
    [contr setHorizontalSeparateLineSize:2];
    // Determine color and thickness of vertical separator line
    [contr setVerticalSeparateLineColor:[UIColor redColor]];
    [contr setVerticalSeparateLineSize:2];
    // Determine table rotation icons
    [contr setFullRotationGestureImage:[UIImage imageNamed:@"full_gesture.png"]];
    [contr setHalfRotationGestureImage:[UIImage imageNamed:@"half_gesture.png"]];
    // Enable displaying of table rotation icons
    [contr setShowRotatingImages:YES];
    // Disable handling of events related to data source refresh
    [contr setSendEventBeforeAndAfterDataSourceUpdate: NO];
    // Set column width
    colWidth = 80;
    // Set row height
    rowHeight = 50;
    // Create a table formatting theme
    NuGridTheme *simpleTheme = [[NuGridTheme new] autorelease];
    // Create a header style
    NuGridCellStyle *headerStyle = [[NuGridCellStyle new] autorelease];
    // Determine background color
    [headerStyle setBackgroundColor:[UIColor
    colorWithRed:0.90 green:0.91 blue:0.96 alpha:1]];
    // Determine font type and size
    [headerStyle setFont:[UIFont fontWithName:@"Arial" size:14]];
    [headerStyle setTextColor:[UIColor whiteColor]];
    // Align text by center
    [headerStyle setTextAlignment:UITextAlignmentCenter];
    [headerStyle setLineBreakMode:UILineBreakModeWordWrap];
    [headerStyle setTextColor:[UIColor blackColor]];
    [headerStyle setStyleName:@"Table header style"];
    [headerStyle setHighlightTextColor:[UIColor yellowColor]];
    // Add style to formatting theme
    [simpleTheme setDefaultRowHeaderStyle:headerStyle];
    [simpleTheme setDefaultColumnHeaderStyle:headerStyle];
    
    // Determine style for other table default cells
    NuGridCellStyle *cellStyle = [NuGridCellStyle deafultStyle];
    // Remove images used by collapsed and expanded expander
    [cellStyle removeExpandedImage];
    [cellStyle removeCollapsedImage];
    [cellStyle setExpanderImageMargin:0];
    // Set default style
    [simpleTheme setDefaultStyle:cellStyle];
    // Set table styles
    [proxyDatasource gridView:(NuGridView *)[contr view] setTheme:simpleTheme];
    // Execute custom example
    [self executeExample];
    // Add created table to application
    [self.view addSubview:[contr gridView]];
}
// Executes custom example placed in the body of this method
-(void) executeExample {
    
};
- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [contr.gridView setFrame:self.view.frame];
}
// Handles menu item selection event
- (void) nuMenu:(NuMenu *)menu selectedItem:(int)itemRow {
    UIMenuItem *item = [[menu items] objectAtIndex:itemRow];
    [[[contr gridView] gridDelegate] performSelector:item.action withObject:item];
    [self dismissPopover];
}
- (void) nuMenuCanceled:(NuMenu *)menu
{
    [self dismissPopover];
}
// Removes popup window in table
- (void) dismissPopover {
    [popover dismissPopoverAnimated:YES];
    [popover release];
    popover = nil;
    if ([[contr gridView] respondsToSelector:@selector(setScrollEnabled:)]) {
        [(UIScrollView *)[contr gridView] setScrollEnabled:YES];
    }
};
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}
// Enables automatic changing of screen orientation
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    NuGridView *view = contr.gridView;
    CGRect frame;
    if(interfaceOrientation == UIInterfaceOrientationPortrait)
    frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
    else
    frame = CGRectMake(0, 0, self.view.frame.size.height, self.view.frame.size.width);
    view.frame = frame;
    return YES;
}
// Returns column width
- (double)gridView:(NuGridView *)gridView widthForColumn:(NSInteger)columnNumber
{
    return colWidth;
}
// Returns row height
- (double) gridView:(NuGridView *)gridView heightForRow:(NSInteger)rowNumber
{
    return rowHeight;
}
// Returns column header height
-(double) gridView:(NuGridView *)gridView heightForHeaderColumnWithNumber:(NSInteger)number
{
    return 50 / 1.5;
}
// Returns row header width
- (double) gridView:(NuGridView *)gridView widthForHeaderRowWithNumber:(NSInteger)number
{
    return 80 / 1.5;
}
// Returns whether column is fixed
- (BOOL)gridView:(NuGridView *)gridView isColumnWithIndexFixed:(NSInteger)columnIndex
{
    return NO;
}
// Returns whether row is fixed
- (BOOL)gridView:(NuGridView *)gridView isRowWithIndexFixed:(NSInteger)rowIndex
{
    return NO;
}
@end

See also:

Creating a Simple Data Grid