2012-01-21 30 views

回答

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

希望它能帮助。

+0

感谢@ H2CO3,它适用于我更新单个单元格时,但是当您想要在所有单元格处于编辑模式时应用此方法吗? (意思是当所有行上出现“减号”按钮时)? – Stan92

+0

我发现这隐藏按钮: - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath :(NSIndexPath *)indexPath {(CustomCell *) [tableView cellForRowAtIndexPath:indexPath] hideButtons]; return UITableViewCellEditingStyleDelete; } 但是,当我离开编辑模式时,不知道将它们显示出来。 – Stan92

+0

无论如何,当您更新单元格时不会调用此方法。它在其他委托方法中被调用。 – 2012-01-21 21:23:49

2

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

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:方法之后调用。