Search code examples
ios4uitableview

iOs - hide buttons when entering edit mode


I have an UITableView that contains 2 buttons for each UITableviewCell. How to hide the buttons when the UITableview is in edit mode? Thanks


Solution

  • I suggest you to subclass UITableViewCell and add the buttons as properties, then set their hiddenproperty to 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
    

    And in your 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];
    }
    

    Hope it helps.