Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hide label in TableViewCell when swipe to delete

I want to be able to hide a label in my UITableViewCell in order to stop it from overlapping with the title whenever a user swipes to delete a cell.

I'm using the following code to initiate and handle the swipe to delete:

- (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've assigned the label in the cell using:

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

And have tried just setting

projectDate.hidden = YES; 

however, this does not work.

like image 445
jcrowson Avatar asked Dec 28 '25 16:12

jcrowson


1 Answers

I think you'll need to subclass UITableViewCell to implement this. In 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, then 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 let you replace text, which might 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, please note that I have two methods with the same name. That obviously is a big no-no....use only one of them.

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

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

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

like image 109
Aaron Hayman Avatar answered Dec 30 '25 04:12

Aaron Hayman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!