Hide shortcut in TableViewCell on delete

I want to be able to hide the label in mine UITableViewCell

so that it doesn't overlap with the title whenever the user loops to delete a cell.

I am using the following code to run and process the deleted file:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {

    if (editingStyle == UITableViewCellEditingStyleDelete) {

        [self.tableView beginUpdates]; // Avoid  NSInternalInconsistencyException

        // Delete the project object that was swiped
        Project *projectToDelete = [self.fetchedResultsController objectAtIndexPath:indexPath];
        NSLog(@"Deleting (%@)", projectToDelete.name);
        [self.managedObjectContext deleteObject:projectToDelete];
        [self.managedObjectContext save:nil];

        // Delete the (now empty) row on the table
        [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];
        [self performFetch];

        [self.tableView endUpdates];
    }
}

      

I assigned a shortcut in a cell using:

UILabel *projectDate = (UILabel *)[cell viewWithTag:3];
    projectDate.text = project.dateStarted;

      

And I tried to simply install

projectDate.hidden = YES; 

      

however it doesn't work.

+3


source to share


1 answer


I think you will need a subclass UITableViewCell

to implement this. The subclass override - (void) setEditing:(BOOL)editing animated:(BOOL)animated

. In this method, you can hide the label. If you only need to hide the label for delete operations, use self.editingStyle

to conditionally hide the label depending on the editing style (aka: UITableViewCellEditingStyleDelete).

Here are two examples. I prefer example two, it's easier. But example one will allow you to replace the text, which may be useful:

@implementation CellSubclass{
    NSString *_labelText; //only used in example 1
}

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
    }
    return self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}
// Example 1, replacing the text value
- (void) setEditing:(BOOL)editing animated:(BOOL)animated{
    [super setEditing:editing animated:animated];
    if (editing && self.editingStyle == UITableViewCellEditingStyleDelete){
        UILabel *label = (UILabel *)[self viewWithTag:3];
        _labelText = label.text;
        self.textLabel.text = nil;
    }  else if (!editing && _labelText){
        UILabel *label = (UILabel *)[self viewWithTag:3];
        label.text = _labelText;
    }
}

//Example 2 - hiding the view itself
- (void) setEditing:(BOOL)editing animated:(BOOL)animated{
    [super setEditing:editing animated:animated];
    if (editing && self.editingStyle == UITableViewCellEditingStyleDelete){
        [self viewWithTag:3].alpha = 0.0f;
    } else {
        [self viewWithTag:3].alpha = 1.0f;
    }
}

@end

      

Please note that I have two methods with the same name. This is obviously a big no-no .... only use one of them.



Also note that I ignored the animated parameter. If you want the fade away of your shortcut to be animated in the second example (aka ... fade away / fade in), all you have to do is surround your changes in an animation block, for example:

        [UIView animateWithDuration:.3f animations:^{
            [self viewWithTag:3].alpha = 0.0f;
        }]; 

      

I don't think you can animate the first example.

+6


source







All Articles