Applying Conditional Formatting for Whole Table (Example 2)

Operating system requirements: iOS 5.0 or later.

Mobile device: iPad.

Description

This example describes conditional formatting setup for the express report whole table. 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):

MAExpressAnalysisReportViewController *contr = (MAExpressAnalysisReportViewController *)m_controller;
// 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 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 views is a table view
            if([subview isMemberOfClass:[NuGridView class]])
            {
                // Get table view object
                NuGridView *gridView = (NuGridView *)subview;
                // Get table delegate
                MAGridDelegate *delegate = (MAGridDelegate*)[gridView gridDelegate];
                // Get proxy data source
                MAGridProxyDataSource *proxyDatasource = (MAGridProxyDataSource*)[delegate proxyDataSource];
                
                // Determine conditional formatting settings
                MAConditionalFormattingScale *formatting = [[MAConditionalFormattingScale new] autorelease];
                // Set identifier of conditional formatting settings
                [formatting setUid: 1008];
                // Apply conditional formatting to the whole table
                [formatting setArea: kCFAEntireTable];
                // Set conditional formatting type
                [formatting setType: kCFTSpecificColors];
                
                // Set fill color for scale elements without data
                [MAConditionalFormattingScale setNoDataColor:[UIColor grayColor]];
                
                // Create a value scale
                ValueScale *preparedData = [[ValueScale new] autorelease];
                // Get standard color palette
                NSArray *defaultPaletteColors= [[SettingsPaletteSelector palettes] objectAtIndex:0];
                // Set scale value
                [preparedData setScaleValues: defaultPaletteColors];
                // Specify the number of scale values
                [preparedData setScaleSteps: [defaultPaletteColors count]];
                // Specify scale type
                [preparedData setScaleType: ValueScaleEqual];
                
                // Create a conditional formatting setting pool
                MAConditionalFormattingPool *pool = [[MAConditionalFormattingPool new] autorelease];
                // Add scale settings to pool
                [pool putPreparedData: preparedData forScaleInRow: nil andColumn:nil withArea: [formatting area]];
                
                // Get scale settings
                ValueScale *scale = [pool preparedDataForScaleInRow:nil andColumn:nil withArea: [formatting area]];
                // Apply scale conditional formatting settings
                [formatting setValue:scale forKey:@"m_scale"];
                
                // Create a helper for working with conditional formatting
                MAConditionalFormattingHelper *formattingHelper = [[MAConditionalFormattingHelper alloc] initWithProxyDataSource: proxyDatasource];
                
                // Create an object for working with conditional formatting
                MAConditionalFormattingScaleLocalHelper *scaleHelper = [[MAConditionalFormattingScaleLocalHelper new] autorelease];
                NSMutableDictionary *settings = [[scaleHelper gridSettingsDictionaryFrom: formatting] mutableCopy];
                [settings setValue: [MAConditionalFormattingScale noDataColor] forKey:@"kGridColorNoData"];
                // Apply scale settings
                formatting = [scaleHelper setupFromGridSettingsDictionary: settings];
                
                // Apply conditional formatting settings
                [formattingHelper applyGlobalConditionalFormat: formatting];
                
                // Delete settings from conditional formatting pool
                [pool invalidatePool];
                NSMutableDictionary *scalesData = (NSMutableDictionary *)[pool valueForKey: @"scalesData"];
                if ([scalesData count] == 0) {
                     NSLog(@"Settings are successfully deleted from conditional formatting pool");
                }
            }
        }
    }
}

After executing the example conditional formatting is applied to the whole table. The palette containing yellow color and shades of green and red colors is used for cell fill. Database cells are filled with grey color:

The development environment console displays a notification that settings are successfully deleted from the conditional formatting pool after their application.

See also:

Example of Component Use