2012-01-21 67 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:方法之後調用。