iOs - скрывать кнопки при входе в режим редактирования - PullRequest
3 голосов
/ 21 января 2012

У меня есть UITableView, который содержит 2 кнопки для каждой UITableviewCell. Как скрыть кнопки, когда UITableview находится в режиме редактирования? Спасибо

Ответы [ 2 ]

3 голосов
/ 22 мая 2015

Просто хотел обновить эту ветку гораздо более простым решением. Чтобы скрыть определенные элементы в пользовательском подклассе UITableViewCell, просто переопределите один метод для UITableViewCell (реализация в 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
}

Это будет вызываться автоматически для каждой ячейки после вызова родительского метода UITableView -setEditing:animated:.

2 голосов
/ 21 января 2012

Я предлагаю вам создать подкласс 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];
}

Hopeэто помогает.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...