Applying Rule-Based Conditional Formatting (Example 3)

Operating system requirements: iOS 5.0 or later.

Mobile device: iPad.

Description

This example describes conditional formatting setup for table cells based on the rule "Value between A and B". Helpers that work with the table cells are used to determine rule properties and rule-based conditional formatting settings.

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];
// Determine values of A and B
double valueA = 7000;
double valueB = 8000;
// 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];
                
                // Create a conditional formatting rule
                MAConditionalFormattingRule *formattingRule = [[MAConditionalFormattingRule new] autorelease];
                // Set type of the rule "Value between A and B"
                [formattingRule setType: kCFRTBetween];
                
                // Create a helper that works with table cells to determine rule properties
                MAConditionalFormattingRuleLocalHelper *ruleLocalHelper = [[MAConditionalFormattingRuleLocalHelper new] autorelease];
                // Get rule settings as a dictionary
                NSMutableDictionary *ruleSettings = (NSMutableDictionary*)[ruleLocalHelper gridSettingsDictionaryFrom: formattingRule];
                // Set fill color for cells that satisfy the rule
                [ruleSettings setValue:[UIColor colorWithRed:1 green:0.77 blue:0.27 alpha:1] forKey:@"kGridHeaderMenuRulesABColor"];
                // Mark created rule as active
                [ruleSettings setValue: [NSNumber numberWithBool: YES] forKey: @"kGridHeaderMenuRulesAB"];
                // Load conditional formatting rule from dictionary
                [ruleLocalHelper setupRule:formattingRule fromGridSettingsDictionary:ruleSettings];
                
                // Initialize rule-based conditional formatting settings
                MAConditionalFormattingRules *rules = [[MAConditionalFormattingRules new] autorelease];
                // Get the rule "Value between A and B"
                MAConditionalFormattingRule *betweenRule = [rules ruleByType: [formattingRule type]];
                // Merge rules
                [betweenRule merge: formattingRule];
                
                /* To determine rule-based conditional formatting settings,
                create a helper that works with table cells */
                MAConditionalFormattingRulesLocalHelper *rulesLocalHelper = [[MAConditionalFormattingRulesLocalHelper new] autorelease];
                // Get conditional formatting rule settings as a dictionary
                NSMutableDictionary *rulesSettings = (NSMutableDictionary*)[rulesLocalHelper gridSettingsDictionaryFrom: rules];
                // Set values of A and B
                [rulesSettings setValue:[NSNumber numberWithDouble: valueA] forKey:@"kGridHeaderMenuRulesABValueA"];
                [rulesSettings setValue:[NSNumber numberWithDouble: valueB] forKey:@"kGridHeaderMenuRulesABValueB"];
                // Get conditional formatting settings from dictionary
                rules = [rulesLocalHelper setupFromGridSettingsDictionary: rulesSettings];
                
                // Create a basic helper for working with conditional formatting settings
                MAConditionalFormattingHelper *formattingHelper = [[MAConditionalFormattingHelper alloc] initWithProxyDataSource: proxyDatasource];
                // Apply consitional formatting settings to the whole table
                [formattingHelper applyGlobalConditionalFormat: rules];
            }
        }
    }
}

After executing the example the conditional formatting with the use of the rule "Cells between A and B" is applied to the express report whole table. All the cells containing values from 7000 to 8000 are filled with yellow color:

See also:

Example of Component Use