Working with Cell Containing Checkbox

Operating system requirements: iOS 5.0 or later.

Mobile device: iPad.

Description

This example describes some methods of working with a cell containing checkbox. 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 instead of the executeExample method of the ViewController class (see the Displaying of Express Report section):

-(void)executeExample {
    // Get express report delegate
    MAExpressAnalysisReportViewController *contr = (MAExpressAnalysisReportViewController *)m_controller;
    
    // Create a cell selection controller
    ElementsViewController *elementDSViewCtrl = [[ElementsViewController alloc] initWithDelegate: contr];
    // Create a checkbox cell
    SettingsCheckmarkedCell *xCell = [[SettingsCheckmarkedCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier:nil];
    // Set delegate
    [xCell setDelegate: elementDSViewCtrl];
    // Specify cell key
    [xCell setKey:@"X_METRIC"];
    // Set title
    [[xCell textLabel] setText: NSLocalizedString(@"X", @"")];
    // Place checkbox to the right
    [xCell setRightCheckmark: YES];
    // Enable to deselect checkbox
    [xCell setMarkOnly: NO];
    // Simulate checkbox tap in five seconds
    [self performSelector:@selector(tapped:) withObject:xCell afterDelay:5];
        
    SettingsCheckmarkedCell *yCell = [SettingsCheckmarkedCell checkmarkedCellWithTitle:NSLocalizedString(@"Y", @"") delegate:elementDSViewCtrl key:@"Y_METRIC"];
    // Enable to deselect checkbox
    [yCell setMarkOnly: NO];
    
    SettingsCheckmarkedCell *colorCell = [SettingsCheckmarkedCell checkmarkedCellWithTitle:NSLocalizedString(@"Color", @"") delegate:elementDSViewCtrl key:@"COLOR_METRIC"];
    // Deny to deselect checkbox
    [colorCell setMarkOnly: YES];
    
    SettingsCheckmarkedCell *sizeCell = [SettingsCheckmarkedCell checkmarkedCellWithTitle:NSLocalizedString(@"Size", @"") delegate:elementDSViewCtrl key:@"SIZE_METRIC"];
    // Deny to deselect checkbox 
    [sizeCell setMarkOnly: YES];
    
    // Add cells to controller
    [elementDSViewCtrl addCell:xCell atRow:0 inSection:0 withSelector:nil];
    [elementDSViewCtrl addCell:yCell atRow:1 inSection:0 withSelector:nil];
    [elementDSViewCtrl addCell:colorCell atRow:2 inSection:0 withSelector:nil];
    [elementDSViewCtrl addCell:sizeCell atRow:3 inSection:0 withSelector:nil];
    
    // Set new values for cells by their keys
    [xCell setChecked: YES];
    [elementDSViewCtrl setSettingsBoolValue: NO forKey:@"Y_METRIC"];
    [elementDSViewCtrl setSettingsDoubleValue: [[NSNumber numberWithBool: NO] doubleValue] forKey:@"COLOR_METRIC"];
    [elementDSViewCtrl setSettingsIntValue:[[NSNumber numberWithBool: YES] intValue] forKey:@"SIZE_METRIC"];
    
    NSLog(@"State of the element X_METRIC: %@", [elementDSViewCtrl settingsValueForKey:@"X_METRIC"]);
    NSLog(@"State of the element Y_METRIC: %d", [elementDSViewCtrl settingsBoolValueForKey:@"Y_METRIC"]);
    NSLog(@"State of the element COLOR_METRIC: %d", [elementDSViewCtrl settingsIntValueForKey:@"COLOR_METRIC"]);
    NSLog(@"State f the element SIZE_METRIC: %f", [elementDSViewCtrl settingsDoubleValueForKey:@"SIZE_METRIC"]);
    
    // Place checkboxes to the right for labels with the keys X_METRIC and Y_METRIC
    [elementDSViewCtrl performSettingsAction:@"X_METRIC" withParameter: [NSNumber numberWithBool: YES]];
    [elementDSViewCtrl performSettingsAction:@"Y_METRIC"];
    // Place checkboxes to the right for labels with the keys Y_METRIC and COLOR_METRIC
    [elementDSViewCtrl performSettingsAction:@"COLOR_METRIC" withParameter: [NSNumber numberWithBool: NO]];
    [elementDSViewCtrl performSettingsAction:@"SIZE_METRIC" withParameter: [NSNumber numberWithBool: NO]];
    // Delete all window subviews
    NSArray *subviews = [self.view subviews];
    for (UIView *subView in subviews) {
        [subView removeFromSuperview];
    }
    // Display controller view for selecting from several elements
    [self.view addSubview: [elementDSViewCtrl view]];
}
// Handles checkbox tap in settings menu cell
- (void)tapped:(NSObject *)object {
    SettingsCheckmarkedCell *cell = (SettingsCheckmarkedCell *)object;
    // Handle checkbox tap
    [cell tapped:nil];
    // Inform delegate on cell state change
    [(ElementsViewController*)[cell delegate] checkStatusChangedInCell:cell];
}

After executing the example the controller view for creating a menu containing cells with the titles X, Y, Color, Size is displayed:

The first and second cell checkboxes can be selected and deselected, the third and fourth cell checkboxes can only be selected. The cells with the X_METRIC and Y_METRIC keys have checkboxes on the left, the cells with the COLOR_METRIC and SIZE_METRIC keys have checkboxes on the right.

Checkboxes for the cells with the X_METRIC and SIZE_METRIC keys are selected, checkboxes for the other cells are deselected. The development environment console displays states of all the cells:

State of the element X_METRIC: 1

State of the element Y_METRIC: 0

State of the element COLOR_METRIC: 0

State of the element SIZE_METRIC: 1.000000

The checkbox in the cell with the X_METRIC key is deselected in five seconds, and the development environment console displays the current state of this cell.

See also:

Example of Component Use