简体   繁体   English

滑动删除时隐藏TableViewCell中的标签

[英]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. 我希望能够在我的UITableViewCell中隐藏标签,以便在用户滑动删除单元格时阻止它与标题重叠。

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 UITableViewCell to implement this. 我认为你需要UITableViewCell来实现它。 In the subclass override - (void) setEditing:(BOOL)editing animated:(BOOL)animated . 在子类重写- (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). 如果您只需要隐藏删除操作的标签,则使用self.editingStyle根据编辑样式有条件地隐藏标签(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. 我认为你不能动画第一个例子。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM