Saturday, June 8, 2013

UITableViewCell setEditingAccessoryView example in Objective C (iOS).

UITableViewCell setEditingAccessoryView

A view that is used typically as a control on the right side of the cell when it is in editing mode.

@property(nonatomic, retain) UIView *editingAccessoryView

Discussion of [UITableViewCell setEditingAccessoryView]
If the value of this property is not nil, the UITableViewCell class uses the given view for the accessory view in the table view’s editing state; it ignores the value of the editingAccessoryType property. The provided accessory view can be a framework-provided control or label or a custom view. The accessory view appears in the right side of the cell.[UITableViewCell setEditingAccessoryView]

The accessory type cross-fades between normal and editing states if it set for both states; use the accessoryType property to set the accessory view for the cell during the table view’s normal state. If this property is not set for both states, the cell is animated to slide or out, as necessary.

UITableViewCell setEditingAccessoryView example.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

UITableViewCell *cell = nil;
NSString *cellDetail = nil;

        static NSString *EnabledCellIdentifier = @"Enabled";
        cell = [tableView dequeueReusableCellWithIdentifier:EnabledCellIdentifier];
        if (cell == nil) {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:EnabledCellIdentifier] autorelease];
            UISwitch* actSwitch = [[UISwitch alloc] initWithFrame:CGRectZero];
            [cell setEditingAccessoryView:actSwitch];
            [actSwitch addTarget:self action:@selector(actSwitchChanged:) forControlEvents:UIControlEventValueChanged];
            if ([[currentItem valueForKey:@"itemEnabled"] boolValue]) {
                cellDetail = @"Active";
                actSwitch.on = YES;
            } else {
                cellDetail = @"Disabled";
                actSwitch.on = NO;
            [actSwitch release];

    cell.textLabel.text = cellDetail;

return cell;

Example of [UITableViewCell setEditingAccessoryView].
                        cell.textLabel.text = @"Limit passwords
                        UISwitch* actSwitch = [[UISwitch alloc]
initWithFrame: CGRectZero ];

                        [cell setEditingAccessoryView: actSwitch];

                        [actSwitch setTag: indexPath.section];
                        [actSwitch addTarget: self

                        [self.view addSubview:actSwitch];
                        [actSwitch setOn:YES animated:NO];
                        [actSwitch setOn:NO animated:NO];

                        actSwitch.on = NO;
                        [cell addSubview: actSwitch];
                        cell.accessoryView = actSwitch;
                        [actSwitch release];


UITableViewCell setEditingAccessoryView example.
 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
     if (editingStyle == UITableViewCellEditingStyleDelete) {
         // Delete the row from the data source
         NSLog(@"Am I Editing");

         UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];

         selectedCell.editingAccessoryView = AccessoryView;
         AccessoryView.backgroundColor = [UIColor clearColor]
     else if (editingStyle == UITableViewCellEditingStyleInsert) {
         // Create a new instance of the appropriate class,
         //   insert it into the array, and add a new row to the table view

End of UITableViewCell setEditingAccessoryView example article.