Saturday, June 8, 2013

UITableViewCell editingAccessoryView example in Objective C (iOS).


UITableViewCell editingAccessoryView

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 editingAccessoryView]
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 editingAccessoryView]

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 editingAccessoryView example.
    UIView *editingCategoryAccessoryView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 120, 35)];

    UIButton *addCategoryButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [addCategoryButton setTitle:@"Add" forState:UIControlStateNormal];
    [addCategoryButton setFrame:CGRectMake(0, 0, 50, 35)];
    [addCategoryButton addTarget:self action:@selector(addCategoryClicked:withEvent:) forControlEvents:UIControlEventTouchUpInside];

    UIButton *removeCategoryButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [removeCategoryButton setTitle:@"Remove" forState:UIControlStateNormal];
    [removeCategoryButton setFrame:CGRectMake(55, 0, 65, 35)];
    [removeCategoryButton addTarget:self action:@selector(removeCategoryClicked:withEvent:) forControlEvents:UIControlEventTouchUpInside];

    [editingCategoryAccessoryView addSubview:addCategoryButton];
    [editingCategoryAccessoryView addSubview:removeCategoryButton];
    cell.editingAccessoryView = editingCategoryAccessoryView;

Example of [UITableViewCell editingAccessoryView].
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
        cell.editingAccessoryView = accessoryView;

       }

}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {

   return NO;

}

UITableViewCell editingAccessoryView 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 editingAccessoryView example article.