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.
I think you’ll need to subclass
UITableViewCellto 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 useself.editingStyleto 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:
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:
I don’t think you can animate the first example.