Custom Editing AccessoryView not working

I have the following code for a UITableView with a custom cell:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"FolderCellViewController"]; if (cell == nil) { // Load the top-level objects from the custom cell XIB. NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"FolderCellViewController" owner:self options:nil]; // Grab a pointer to the first object (presumably the custom cell, as that all the XIB should contain). cell = [topLevelObjects objectAtIndex:0]; cell.editingAccessoryView=accessoryView; //accessoryView is a UIView within a UITableViewCell, and it is properly connected in IB cell.selectionStyle = UITableViewCellSelectionStyleNone; } return cell; } // Override to support conditional editing of the table view. - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { // Return NO if you do not want the specified item to be editable. return NO; //YES here makes a red delete button appear when I swipe } // Override to support editing the table view. - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { if (editingStyle == UITableViewCellEditingStyleDelete) { // Delete the row from the data source // [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; } 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 } } 

But for some, when I do nothing, nothing happens. I have done nothing but this, is there anything else I need to do for this?

EDIT: Apparently, I only set the editing style when the whole table is in edit mode, and not when I scroll through each individual cell. So what I want to do, when I scroll through each cell, a custom accessory appears for that cell. But I'm not sure how to do this.

+7
source share
1 answer

The view of the editing accessory is displayed when the cell enters editing mode. It seems like it is too complicated to actually get this work, but I managed it:

In order to display this both when entering editing mode for the entire table, and when scrolling through a single row, I performed the following in my subclass of UITableViewController:

 - (void)setEditing:(BOOL)editing animated:(BOOL)animated { if (editing) self.editingFromEditButton = YES; [super setEditing:(BOOL)editing animated:(BOOL)animated]; self.editingFromEditButton = NO; // Other code you may want at this point... } 

editingFromEditButton is a BOOL property of a subclass. This method is called when the standard "Edit" button is clicked. It is used in the following method, which disables the standard delete button:

 - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath { if (self.editingFromEditButton) return UITableViewCellEditingStyleNone; // Otherwise, we are at swipe to delete [[tableView cellForRowAtIndexPath:indexPath] setEditing:YES animated:YES]; return UITableViewCellEditingStyleNone; } 

If the entire view of the table is set to edit mode, then each cell will also be sent a setEditing message. If we checked a single row, we need to make this cell go into edit mode, and then return the UITableViewCellEditingStyleNone style to prevent the standard delete button from appearing.

Then, to abandon the special editing accessory, you will also need the following code:

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { // Cancel the delete button if we are in swipe to edit mode UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; if (cell.editing && !self.editing) { [cell setEditing:NO animated:YES]; return; } // Your standard code for when the row really is selected... } 
+10
source

All Articles