Reputation: 453
I have an UITableView that contains 2 buttons for each UITableviewCell. How to hide the buttons when the UITableview is in edit mode? Thanks
Upvotes: 4
Views: 2860
Reputation: 4105
Just wanted to update this thread with a much simpler solution. In order to hide specific elements in a custom subclass of UITableViewCell
, simply override one method for UITableViewCell
(implementation in 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
}
This will be called automatically for each cell after you call the parent UITableView
's -setEditing:animated:
method.
Upvotes: 5
Reputation:
I suggest you to subclass UITableViewCell and add the buttons as properties, then set their hidden
property 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.
Upvotes: 2