Working with Table Proxy Data Source

Operating system requirements: iOS 5.0 or later.

Mobile device: iPad.

Description

This example describes some methods of working with table proxy data source. After starting the example the following operations are executed:

Source Code

Executing the example requires to place the following code in the body of the executeExample method of the ViewController class (see the Displaying of Express Report section):

// Get array of data view controllers
NSArray *controllers =[m_controller dataViewControllers];
// Parse data view controllers in cycle
for(NSObject *controller in controllers)
{
    // Check if the current controller is a table data view controller
    if([controller isMemberOfClass:[MAGridDataViewController class]])
    {
        // Get table data view controller object
        MAGridDataViewController *gridController = (MAGridDataViewController *)controller;
        // Get controller view
        UIView *view = [gridController view];
        // Get array of subviews
        NSArray *subviews = [view subviews];
        // Parse subviews in cycle
        for(NSObject *subview in subviews)
        {
            // Check if the current view is a table view
            if([subview isMemberOfClass:[NuGridView class]])
            {
                // Get table view object
                NuGridView *gView = (NuGridView *)subview;
                // Get table delegate object
                MAGridDelegate *gridDelegate = (MAGridDelegate *)[gView gridDelegate];
                // Get table proxy data source
                MAGridProxyDataSource *proxyDatasource =[gridDelegate proxyDataSource];
                // Save initial state of proxy data source
                NSData *state = [proxyDatasource saveState];
                // Display whether specified column width and specified row height are fixed in the development environment console
                NSLog(@"Width of column 1 %@is fixed", [proxyDatasource isWidthFixedForColumnWithKey: 1]? @"":@"not ");
                NSLog(@"Height of row 1 %@is fixed", [proxyDatasource isHeightFixedForRowWithKey:1]? @"":@"not ");
                // Fix cell height for specified row
                [proxyDatasource fixHeight:20 forRowWithKey:1];
                // Fix cell width for specified column
                [proxyDatasource fixWidth:40 forColumnWithKey:1];
                // Display message about adding fixed height row in the development environment console
                NSLog(@"Adding of row with fixed height");
                // Display message about adding fixed width column in the development environment console
                NSLog(@"Adding of fixed width column");
                // Display whether specified column width and specified row height are fixed in the development environment console
                NSLog(@"Width of column 1 %@is fixed", [proxyDatasource isWidthFixedForColumnWithKey: 1]? @"":@"not ");
                NSLog(@"height of row 1 %@is fixed", [proxyDatasource isHeightFixedForRowWithKey:1]? @"":@"not ");
                // Get table data source
                GridDataViewDatasource *dataSource =[proxyDatasource dataSource];
                // Display message about whether data source is empty in the development environment console
                NSLog(@"Is table data source empty? %@", [dataSource isEmpty]? @"yes":@"no");
                // Get table delegate
                MAGridDelegate *gridDelegateFromProxy = [proxyDatasource gridDelegate];
                // Display title of linked table data view controller obtained by means of delegate
                NSLog(@"Title of linked table data view controller: %@",[[gridDelegateFromProxy dataViewController] title]);
                // Get and display cell indent in specified header column in the development environment console
                NSInteger columnIndent = [proxyDatasource gridView:gView indentForHeaderForColumn:0 number:0];
                NSLog(@"Cell indent in header column with the 0 index: %d", columnIndent);
                // Get and display cell indent in specified header row in the development environment console
                NSInteger rowIndent = [proxyDatasource gridView:gView indentForHeaderForRow:1 number:0];
                NSLog(@"Cell indent in header row with the 1 index: %d", rowIndent);
                // Select set table cells
                [[gView controller] selectAreaFromRow:[gView rowWithNumber:0] column:[gView columnWithNumber:0] toRow:[gView rowWithNumber:1] column:[gView columnWithNumber:1]];
                // Get arrays of selected table rows and columns
                NSSet *selectedRows = [[NSMutableSet new] autorelease];
                NSSet *selectedColumns = [[NSMutableSet new] autorelease];
                NuGridSelectionArea *selectionArea = gView.selectionArea;
                for (NuGridRow *row in selectionArea.cellArea.rows)
                {
                    [(NSMutableSet *)selectedRows addObject:[NSNumber numberWithInt:row.rowNumber]];
                }
                for (NuGridColumn *column in selectionArea.cellArea.columns) {
                    [(NSMutableSet *)selectedColumns addObject:[NSNumber numberWithInt:column.columnNumber]];
                }
                // Set table cell selection area in proxy data source
                [proxyDatasource setSelectionWithRows:selectedRows columns:selectedColumns];
                // Get table cell selection area from proxy data source
                NSDictionary *gridSelection = [proxyDatasource gridSelection];
                // Display information from obtained dictionary in the development environment console
                NSLog(@"Dictionary type for storing table selection area: %@", [gridSelection valueForKey:@"type"]);
                NSLog(@"Number of selected columns: %d",[[gridSelection valueForKey:@"columns"] count]);
                NSLog(@"Number of selected rows: %d",[[gridSelection valueForKey:@"rows"] count]);
                // Remove table cell selection area
                [proxyDatasource clearGridSelection];
                // Display message about removing table cell selection area in the development environment console
                NSLog(@"Removing table cell selection area from proxy data source");
                // Display numberof selected dictionary elements for storing table selection area in the development environment console
                NSLog(@"Number of selected columns: %d",[[gridSelection valueForKey:@"columns"] count]);
                NSLog(@"Number of selected rows: %d",[[gridSelection valueForKey:@"rows"] count]);
                // Get data without totals for specified row
                NuGridSparklineData *data = [proxyDatasource getRowDataWithoutTotals: 1];
                // Display number and value of the cell containing maximum row value in the development environment console
                NSLog(@"Maximum value of row 1 in the %d cell, value: %f", [data maxPeakElementIndex], [data maxValue]);
                // Determine whether proxy data source is ready
                [proxyDatasource setReady:YES];
                // Execute synchronous update of proxy data source
                [proxyDatasource updateSync];
                // Create and fill the dictionary containing parameters required for the continueExample: method
                NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
                [dict setValue: state forKey:@"state"];
                [dict setValue: proxyDatasource forKey:@"proxyDatasource"];
                // Call after pause the method that continues example execution
                [self performSelector:@selector(continueExample:) withObject:dict afterDelay:5];
            }
        }
    }
}

It is also required to add the following method with its implementation in the ViewController class:

// Continue example execution
- (void) continueExample: (NSDictionary *) dict
{
    // Retrieve table proxy data source from dictionary
    MAGridProxyDataSource *proxyDatasource = [dict valueForKey:@"proxyDatasource"];
    // Retrieve saved state of proxy data source from dictionary
    NSData *state = [dict valueForKey:@"state"];
    // Load proxy data source state
    if(proxyDatasource != nil && state != nil) [proxyDatasource loadState:state];
}

 After executing the example the mobile device screen displays the express report containing the table, for which cell selection is set, and also height and width for the specified rows and columns are changed:

The development environment console also displays information about table proxy data source:

Width of column 1 is not fixed

Height of row 1 is not fixed

Adding of row with fixed height

Adding of column with fixed width

Width of column 1 is fixed

Height of row 1 is fixed

Is table data source empty? no

Title of table linked data view controller: Grid

Indent of cell in header column with the 0 index: 0

Indent of cell in header row with the 1 index: 1

Type of dictionary for storing table selection area: cells

Number of selected columns: 2

Number of selected rows: 2

Removing of table cell selection area from proxy data source

Number of selected columns: 0

Number of selected rows: 0

Maximum value of row 1 in the 10 cell, value: 29763.284441

 

The initial table state is restored in five seconds after executing the example:

Then replace the code string:

[proxyDatasource setReady:YES];

 with the following string:

[proxyDatasource setReady:NO];

This operation is used to set a new value of table proxy data source readiness, which denies synchronous update. After executing the updated example the mobile device screen displays the express report containing the table, for which changes specified in the example code are displayed. The express report appearance in the update example matches the express report external appearance after restoring the saved state of proxy data source.

The cell selection area in the proxy data source cam be obtained by means of table data view controller, to do this, replace the strings:

// Get table cell selection area from proxy data source
NSDictionary *gridSelection = [proxyDatasource gridSelection];

with the following code:

// Get table cell selection area by means of table data view controller
NSDictionary *gridSelection = [gridController gridSelection];

See also:

Examples of Component Use