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

最佳答案

我建议您继承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];
}

希望能帮助到你。

09-07 14:18