Data Grid with Expanders

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

The ViewController.h File

#import <UIKit/UIKit.h>
#import "ProxyDataSource.h"
#import "DataSource.h"
#import <NuGridView/NuGridController.h>
#import <NuGridView/NuGridDelegate.h>
@interface ViewController : UIViewController<NuGridDelegate> {
    DataSource *datasource; // Data source
    ProxyDataSource *proxyDatasource; // Proxy data source
    NuGridController *contr; // Table manager
}
// Executes custom example placed in the body of this method
-(void)executeExample;
@end

The ViewController.m File

#import "ViewController.h"
#import <NuGridView/NuGridCellStyle.h>
#import <NuGridView/NuGridCell.h>
#import <NuGridView/NuGridView.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 that controls table
    contr = [[NuGridController alloc] init];
    // Set the object that controls table and a link to proxy data source
    [contr setDataSource:proxyDatasource];
    // Set the object that controls basic table functionality
    [contr setDelegate:self];
    // Enable the use of fixed rows and columns
    [contr setCanFix:YES];
    [contr setDockable:YES];
    // Create a column style
    NuGridCellStyle *headerStyle = [[NuGridCellStyle new] autorelease];
    // Set 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:12]];
    [headerStyle setTextColor:[UIColor whiteColor]];
    // Align text by center
    [headerStyle setTextAlignment:UITextAlignmentCenter];
    [headerStyle setLineBreakMode:UILineBreakModeWordWrap];
    [headerStyle setTextColor:[UIColor blackColor]];
    // Enable bold face for text in table parent cell
    [headerStyle setBoldForParent:YES];
    // Determine image for expanded cell
    UIImage *expandedImage = [UIImage imageNamed:@"expanded.png"];
    [headerStyle setExpandedImage:expandedImage];
    [headerStyle setExpanderImageAlignment: NuImageVerticalAlignmentCenter];
    [headerStyle setExpanderImageMargin:15];
    // Determine image for collapsed cell
    UIImage *collapsedImage = [UIImage imageNamed:@"collapsed.png"];
    [headerStyle setCollapsedImage:collapsedImage];
    // Determine image for colapsed cell in the mode of text direction from right to left
    UIImage *collapsedRTLImage = [UIImage imageNamed:@"collapsed_rtl.png"];
    [headerStyle setCollapsedRTLImage:collapsedRTLImage];
    
    // Determine style for other default table cells
    NuGridCellStyle *cellStyle = [NuGridCellStyle deafultStyle];
    // Set table styles
    [proxyDatasource gridView:(NuGridView *)[contr view] setDefaultStyle:cellStyle];
    [proxyDatasource gridView:(NuGridView *)[contr view] setDefaultStyleForColumnHeader:headerStyle];
    [proxyDatasource gridView:(NuGridView *)[contr view] setDefaultStyleForRowHeader:headerStyle];
    // Expand expander in the second row
    [proxyDatasource gridView:contr.gridView expandRow:1];
    // Execute custom example
    [self executeExample];
    // Add created table to application
    [self.view addSubview:[contr gridView]];
}
// Executes custom example located in the body of this method
-(void)executeExample{
    
};
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}
// Implements the ability to automatically change 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;
}
- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [contr.gridView setFrame:self.view.frame];
}
// Handles table cell touch
- (void) gridView: (NuGridView *) gridView wasTouchedInCell: (NuGridCell *) cell {
    if ([datasource gridView:gridView parentRowForRow:[cell rowNumber]] > 0 && [cell columnNumber] == 0) {
        if ([datasource rowIsExpanded:[cell rowNumber]]) {
            [proxyDatasource gridView:contr.gridView collapseRow:[cell rowNumber]];
        }
        else
        [proxyDatasource gridView:contr.gridView expandRow:[cell rowNumber]];
    }
}
// Returns column width
- (double)gridView:(NuGridView *)gridView widthForColumn:(NSInteger)columnNumber
{
    return 100;
}
// Returns row height
- (double) gridView:(NuGridView *)gridView heightForRow:(NSInteger)rowNumber
{
    return 40;
}
// Returns column header height
-(double) gridView:(NuGridView *)gridView heightForHeaderColumnWithNumber:(NSInteger)number
{
    return 40;
}
// Returns row header width
- (double) gridView:(NuGridView *)gridView widthForHeaderRowWithNumber:(NSInteger)number
{
    return 85;
}
// 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 Table with Expanders