iOs - 进入编辑模式时隐藏按钮

时间:2012-01-21 16:48:42

标签: ios4 uitableview

我有一个UITableView,每个UITableviewCell包含2个按钮。 如何在UITableview处于编辑模式时隐藏按钮? 感谢

2 个答案:

答案 0 :(得分:3)

只是想用更简单的解决方案更新此线程。为了隐藏UITableViewCell的自定义子类中的特定元素,只需覆盖UITableViewCell的一个方法(在Swift中实现):

override func setEditing(editing: Bool, animated: Bool) {
    super.setEditing(editing, animated: animated)
    // Customize the cell's elements for both edit & non-edit mode
    self.button1.hidden = editing
    self.button2.hidden = editing
}

在您调用父UITableView -setEditing:animated:方法后,将为每个单元格自动调用此方法。

答案 1 :(得分:2)

我建议你继承UITableViewCell并将按钮添加为属性,然后将它们的hidden属性设置为YES:

@interface CustomCell: UITableViewCell
{
    UIButton *btn1;
    UIButton *btn2;
}

@property (nonatomic, readonly) UIButon *btn1;
@property (nonatomic, readonly) UIButon *btn2;

- (void)showButtons;
- (void)hideButtons;

@end

@implementation CustomCell

@synthesize btn1, btn2;

- (id) initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSStrig *)reuseId
{
    if ((self = [super initWithStyle:style reuseidentifier:reuseId]))
    {
        btn1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        // etc. etc.
    }
    return self;
}

- (void) hideButtons
{
    self.btn1.hidden = YES;
    self.btn2.hidden = YES;
}

- (void) showButtons
{
    self.btn1.hidden = NO;
    self.btn2.hidden = NO;
}

@end

在你的UITableViewDelegate中:

- (void)tableView:(UITableView *)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath
{
    [(CustomCell *)[tableView cellForRowAtIndexPath:indexPath] hideButtons];
}

- (void)tableView:(UITableView *)tableView didEndEditingRowAtIndexPath:(NSIndexPath *)indexPath
{
    [(CustomCell *)[tableView cellForRowAtIndexPath:indexPath] showButtons];
}

希望它有所帮助。