Applying Conditional Formatting to Whole Table (Example 1)

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:

Required Files

It is required to add the following file to the base example Displaying of Express Report:

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];
// Declare helper for working with conditional formatting
MAConditionalFormattingHelper *formattingHelper = nil;
// 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 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: 1007];
                // Apply conditional formatting to the whole table
                [formatting setArea: kCFAEntireTable];
                
                MAConditionalFormattingScale *formattingScale = [[MAConditionalFormattingScale new] autorelease];
                // Set conditional formatting type
                [formattingScale setType: kCFTSpecificColors];
                [formattingScale merge: formatting];
                
                // Set fill color for scale elements without data
                [MAConditionalFormattingScale setNoDataColor:[UIColor grayColor]];
                UIColor *noDataColor = [MAConditionalFormattingScale noDataColor];
                const CGFloat *components = CGColorGetComponents([noDataColor CGColor]);
                CGFloat r = components[0];
                CGFloat g = components[1];
                CGFloat b = components[2];
                NSString *hexString=[NSString stringWithFormat:@"%02X%02X%02X", (int)(r * 255), (int)(g * 255), (int)(b * 255)];
                NSLog(@"Fill color for cells without data: #%@", hexString);
                
                // Get value scale
                ValueScale *scale = [formattingScale scale];
                // Set scale value
                [scale setScaleValues: [NSArray arrayWithObject: [MAConditionalFormattingScale noDataColor]]];
                // Specify number of scale values
                [scale setScaleSteps: 1];
                // Specify scale type
                [scale setScaleType: ValueScaleEqual];
                
                // Create an object for working with conditional formatting of whole table
                MAConditionalFormattingScaleGlobalHelper *scaleHelper = [[MAConditionalFormattingScaleGlobalHelper new] autorelease];
                NSMutableDictionary *settings = [[scaleHelper gridSettingsDictionaryFrom: formattingScale] mutableCopy];
                // Get standard color palette
                NSArray *defaultPaletteColors= [[SettingsPaletteSelector palettes] objectAtIndex:0];
                [settings setValue:defaultPaletteColors forKey:@"kGridColorList"];
                [settings setValue:[NSNumber numberWithInt:5] forKey:@"kGridColorGroups"];
                // Apply settings
                formattingScale = [scaleHelper setupFromGridSettingsDictionary: settings];
                
                // Create a helper for working with conditional formatting
                formattingHelper = [[MAConditionalFormattingHelper alloc] initWithProxyDataSource: proxyDatasource];
                ConditionalFormattingObserver *observer = [[ConditionalFormattingObserver new] autorelease];
                [observer setGridView: (NuGridView *)subview];
                // Add observer
                [formattingHelper addObserver: observer];
                // Apply conditional formatting settings
                [formattingHelper applyGlobalConditionalFormat: formattingScale];
                // Remove observer
                [formattingHelper removeObserver: observer];
                if ([formattingHelper globalConditionalFormat] != nil) {
                    NSLog(@"Conditional formatting settings are successfully applied");
                }
            }
        }
    }
}

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 hexadecimal code of fill color of database cells and a notification about successful application of conditional formatting settings:

Fill color for cells without data: #7FFF00

Conditional formatting settings are successfully applied

Delete conditional formatting settings by adding the following code fragment to the example:

[formattingHelper removeConditionalFormatting];

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

See also:

Example of Component Use