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

时间:2012-03-07 15:53:27

标签: ios objective-c uitableview

我希望能够在我的UITableViewCell中隐藏标签,以便在用户滑动删除单元格时阻止其与标题重叠。

我正在使用以下代码启动和处理要删除的滑动:

- (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];
    }
}

我已使用以下内容在单元格中指定了标签:

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

并尝试过设置

projectDate.hidden = YES; 

然而,这不起作用。

1 个答案:

答案 0 :(得分:6)

我认为你需要继承UITableViewCell来实现它。在子类中覆盖- (void) setEditing:(BOOL)editing animated:(BOOL)animated。在此方法中,您可以隐藏标签。如果您只需要隐藏删除操作的标签,请使用self.editingStyle根据编辑样式有条件地隐藏标签(aka:UITableViewCellEditingStyleDelete)。

以下是两个例子。我更喜欢第二个例子,它更容易。但是示例之一将允许您替换可能有用的文本:

@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

请注意,我有两个同名的方法。这显然是一个很大的禁忌......只使用其中一个。

另请注意,我忽略了动画参数。如果你希望在第二个例子(也就是......淡出/淡入)中对你的标签的消失进行动画处理,你需要做的就是在动画块中包围你的变化,如下所示:

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

我认为你不能为第一个例子制作动画。

相关问题