Applying Conditional Formatting for Table Row and Column

Operating system requirements: iOS 5.0 or later.

Mobile device: iPad.

Description

This example describes conditional formatting setup for express report table row and column.

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):

MAExpressAnalysisReportViewController *contr = (MAExpressAnalysisReportViewController *)m_controller;
EAReportDelegateImpl *delegate = [contr delegate];
// Get table data source
GridDataViewDatasource *gridDatasource = (GridDataViewDatasource *)[delegate gridDatasource];
// Get array of data view controllers
NSArray *controllers =[contr 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 *gridView = (NuGridView *)subview;
                // Get delegate for working with table
                MAGridDelegate *delegate = (MAGridDelegate*)[gridView gridDelegate];
                // Get proxy data source
                MAGridProxyDataSource *proxyDatasource = (MAGridProxyDataSource*)[delegate proxyDataSource];
                
                // Get row persisted key
                id<MAGridDataSourcePersistentKeyProtocol> rowPersistentKey = [gridDatasource rowPersistentKeyByNumber: 0];
                id<MAGridDataSourcePersistentKeyProtocol> columnPersistentKey = [gridDatasource columnPersistentKeyByNumber: 0];
                
                // Create a conditional formatting setting pool
                MAConditionalFormattingPool *pool = [[MAConditionalFormattingPool new] autorelease];
                // Declare pool settings
                NSMutableDictionary *scalesData = [NSMutableDictionary dictionary];
                
                // Determine conditional formatting settings for row and column
                MAConditionalFormattingScale *rowFormatting = [[MAConditionalFormattingScale new] autorelease];
                [rowFormatting setArea: kCFARows];
                MAConditionalFormattingScale *columnFormatting = [[MAConditionalFormattingScale new] autorelease];
                [columnFormatting setArea: kCFAColumns];
                
                // Create a value scale for row
                ValueScale *rowScale = [[ValueScale new] autorelease];
                // Get standard color palette
                NSArray *defaultPaletteColorsForRows = [[SettingsPaletteSelector palettes] objectAtIndex:0];
                // Set scale value
                [rowScale setScaleValues: defaultPaletteColorsForRows];
                // Specify the number of scale values
                [rowScale setScaleSteps: [defaultPaletteColorsForRows count]];
                // Specify scale type
                [rowScale setScaleType: ValueScaleEqual];
                
                // Create a value scale for row
                ValueScale *columnScale = [[ValueScale new] autorelease];
                // Get standard color palette
                NSArray *defaultPaletteColorsForColumns = [[SettingsPaletteSelector palettes] objectAtIndex:4];
                // Set scale value
                [columnScale setScaleValues: defaultPaletteColorsForColumns];
                // Specify the number of scale values
                [columnScale setScaleSteps: [defaultPaletteColorsForColumns count]];
                // Specify scale type
                [columnScale setScaleType: ValueScaleEqual];
                
                // Create a key of row conditional formatting setting pool
                MAConditionalFormattingPoolKey *rowPoolKey = [[MAConditionalFormattingPoolKey new] autorelease];
                [rowPoolKey setArea: [rowFormatting area]];
                [rowPoolKey setRuleType: kCFRTGreatest];
                [rowPoolKey setRowKey: rowPersistentKey];
                
                // Create a key of column conditional formatting setting pool
                MAConditionalFormattingPoolKey *columnPoolKey = [[MAConditionalFormattingPoolKey new] autorelease];
                [columnPoolKey setArea: [columnFormatting area]];
                [columnPoolKey setRuleType: kCFRTGreatest];
                [columnPoolKey setColumnKey: columnPersistentKey];
                
                // Add scale setting to pool
                [scalesData setObject:rowScale forKey: rowPoolKey];
                [scalesData setObject:columnScale forKey: columnPoolKey];
                [pool setValue: scalesData forKey:@"scalesData"];
                [rowFormatting setConditionalFormattingPool: pool];
                [columnFormatting setConditionalFormattingPool: pool];
                
                // Prepare row and column conditional formatting settings
                [rowFormatting prepareInRow:rowPersistentKey andColumn: nil forDataSource:proxyDatasource];
                [columnFormatting prepareInRow: nil andColumn:columnPersistentKey forDataSource:proxyDatasource];
                
                // Create a helper for working with conditional formatting
                MAConditionalFormattingHelper *formattingHelper = [[MAConditionalFormattingHelper alloc] initWithProxyDataSource: proxyDatasource];
                // Apply conditional formatting settings to row
                [formattingHelper applyConditionalFormat: rowFormatting toRow: rowPersistentKey];
                // Apply conditional formatting settings to column
                [formattingHelper applyConditionalFormat: columnFormatting toColumn:columnPersistentKey];
                
                if ([formattingHelper conditionalFormatInRow: rowPersistentKey] != nil) {
                    NSLog(@"Row conditional formatting settings are successfully applied");
                }
                if ([formattingHelper conditionalFormatInColumn: columnPersistentKey] != nil) {
                    NSLog(@"Column conditional formatting settings are successfully applied");
                }
            }
        }
    }
}

After executing the example conditional formatting is applied to the first row and column of express report table. The palette containing yellow color and shades of green and red colors is used for row cell fill, the palette containing shades of grey color is used for column cell fill:

The development environment console displays notifications that conditional formatting settings are successfully applied to the table row and column.

Delete conditional formatting settings for the table row and column by adding the following code fragment to the example:

[formattingHelper removeConditionalFormattingFromRow: rowPersistentKey];
[formattingHelper removeConditionalFormattingFromColumn: columnPersistentKey];

After executing the example the express report table is restored to its initial view.

See also:

Example of Component Use