Using Gradient for Cell Conditional Formatting

Operating system requirements: iOS 5.0 or later.

Mobile device: iPad.

Description

This example describes the use of gradient for table cell conditional formatting. 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 Creating a Simple Data Grid section):

// Get style of cell with the coordinate B1
NuGridCellStyle *style = [proxyDatasource gridView:[contr gridView] getStyleForCellInRow:1
column:1];
// Determine gradient colors
UIColor *redColor = [UIColor colorWithRed:0.94 green:0.43 blue:0.46 alpha:1];
UIColor *yellowColor = [UIColor colorWithRed:0.96 green:0.91 blue:0.46 alpha:1];
UIColor *greenColor = [UIColor colorWithRed:0.62 green:0.86 blue:0.29 alpha:1];
// Determine two color schemes
NSMutableArray *colorsTheme = [[NSMutableArray new] autorelease];
[colorsTheme addObject:@[redColor, yellowColor, greenColor]];
[colorsTheme addObject:@[greenColor, yellowColor, redColor]];
// Use the second color scheme
[style setGradientTag:1];
// Determine the number of rows
int rowCount = [datasource gridViewRowCount:[contr gridView]];
int columnNumber = 2;
// Determine array of column values
NSMutableArray *valueArray = [[NSMutableArray new] autorelease];
for (int i = 0; i < rowCount; ++i) {
    NSObject *value = [datasource gridView:[contr gridView] valueForCellInRow:i inColumn:columnNumber];
    [valueArray addObject:value];
    if (valueArray.count > 0) {
        // Sort array values
        [valueArray sortUsingSelector:@selector(compare:)];
        // Determine minimum, medium and maximum values
        style.minGradValue = [[valueArray objectAtIndex:0] doubleValue];
        style.midGradValue = [[valueArray objectAtIndex:valueArray.count / 2] doubleValue];
        style.maxGradValue = [[valueArray lastObject] doubleValue];
    }
}
// Specify that all gradient settings are determined
[style setIsGradientConfigured:YES];
//[style removeGradientTag]; // Defect
// Get color scheme
NSArray *colorArray = colorsTheme[[style gradientTag]];
/* Determine the colors corresponding to minimum, medium
and maximum values */
[style setMinColor:colorArray[0]];
[style setMidColor:colorArray[1]];
[style setMaxColor:colorArray[2]];
[style setGradientColumnBackground:YES];
[style setGradientRowBackground:YES];
// Apply created style
for (int i = 0; i < [datasource gridViewRowCount:[contr gridView]]; i++) {
    for (int j = 0; j < [datasource gridViewColumnCount:[contr gridView]]; j++) {
        [proxyDatasource gridView:[contr gridView] setStyle:style forCellInRow:i column:j];
    }
}

After executing the example conditional formatting is applied to table cells with the use of gradient:

Remove the tag used for gradient formatting and the indicator whether gradient is formed, by adding the following code to the example:

NSLog(@"Conditional formatting tag before removal: %d", [style gradientTag]);
// Remove the tag used for gradient formatting
[style removeGradientTag];
NSLog(@"Conditional formatting tag after removal: %d", [style gradientTag]);
// Remove whether conditional formatting gradient is formed
[style removeIsGradientConfigured];

After executing the example the development environment console displays the conditional formatting tag before and after its removal:

Conditional formatting tag before removal: 1

Conditional formatting tag after removal: -1
 

The table is restored to its initial view:

The identical result is obtained if the colors corresponding to minimum, medium and maximum values of table cells are removed:

[style removeMinColor];
[style removeMidColor];
[style removeMaxColor];

Remove the minimum, medium and maximum values of table cells:

[style removeMinGradValue];
[style removeMidGradValue];
[style removeMaxGradValue];

After executing the example the cells are filled with black color:

See also:

Examples of Component Use