【问题标题】:how to hide custom button when editing mode is enabled?启用编辑模式时如何隐藏自定义按钮?
【发布时间】:2013-07-01 05:46:03
【问题描述】:

我使用 drawRect: 制作了一个自定义按钮,并将它放在我的 headerview 中用于我的 tableview。我希望在选择编辑模式时隐藏自定义按钮。我知道我可以通过以下方法做到这一点:

-(void)setEditing:(BOOL)editing animated:(BOOL)animated

但由于某种原因,当我 1) 将其设置为 nil、2) 或使用 button.hidden 属性时,我的按钮实际上并没有消失。这是我的代码:

TableViewController.h:

@interface ToDoTableViewController : UITableViewController <Properties2ViewControllerDelegate, UITableViewDelegate>{
    addButtonView *button;
}
@property (strong, nonatomic) NSMutableArray *taskArray;
@property (strong, nonatomic) NSMutableArray *completedArray;
-(IBAction)addCell:(id)sender;
-(void)buttonPressed:(id)sender;
@end

TableViewController.m

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *headerView;
    UIView *seperatorView;

    CGRect testFrame = CGRectMake(280.0, 5.0, 30.0, 30.0);
   button = [[addButtonView alloc]initWithFrame:testFrame];

    NSString *sectionTitle = @"Incomplete Tasks";
    NSString *section2Title = @"Completed Tasks";
    UILabel *label = [[UILabel alloc]init];
    label.textColor = [UIColor colorWithRed:236.0/255 green:240.0/255 blue:241.0/255 alpha:1.0f];
    label.font = [UIFont fontWithName:@"HelveticaNeue-Light" size:25];
    label.backgroundColor = [UIColor clearColor];
    label.frame = CGRectMake(10.0, 0.0, 320.0, 40.0);
    headerView = [[UIView alloc]initWithFrame:label.frame];

    [button addTarget:self action:@selector(addCell:) forControlEvents:UIControlEventTouchUpInside];
    [button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];

    CGRect sepFrame = CGRectMake(0, headerView.frame.size.height-2, 320, 1);
    seperatorView = [[UIView alloc] initWithFrame:sepFrame];
    seperatorView.backgroundColor = [UIColor colorWithRed:236.0/255 green:240.0/255 blue:241.0/255 alpha:1.0f];
    [headerView addSubview:seperatorView];

    switch (section) {
        case 0:
            label.text = sectionTitle;
            [headerView addSubview:label];
             [headerView addSubview:button];
            break;
        case 1:
         label.text = section2Title;

            [headerView addSubview:label];
          // if (completedArray == nil)
            //   headerView.hidden = YES;
            break;

    }
    return headerView;
}

-(void)setEditing:(BOOL)editing animated:(BOOL)animated{
    [super setEditing:editing animated:animated];

    if([self isEditing]){
        button.hidden = YES;
    }else {
        button.hidden = NO;
    }
}

---编辑----

-(void)setEditing:(BOOL)editing animated:(BOOL)animated{
    [super setEditing:editing animated:animated];

    if([self isEditing]){
        button.hidden = YES;
        [[self tableView] reloadData]; //shouldn't this make the button dissapear?
    }else {
        button.hidden = NO;
    }
}

【问题讨论】:

    标签: iphone ios objective-c uitableview uibutton


    【解决方案1】:

    每当重新加载表格视图时,您的代码都会重新创建整个标题,包括创建一个新按钮。因此,您可能已将旧按钮设置为隐藏,然后它被销毁并替换为新的可见按钮。

    返回标头时,要么创建一次并始终返回相同的实例,然后更改 hidden 属性应该可以工作。或者,每次创建表头时检查表isEditing 并决定结果如何。


    让我们使用更有效的标头重用选项:

    1. 创建一个属性来保存标题视图 - 一个数组。
    2. 在viewDidLoad 中创建标题视图(使用您当前的代码,但已移至不同的方法)。
    3. 将标题视图添加到属性中(确保先初始化数组)
    4. 将viewForHeaderInSection 更改为仅返回数组中的标头(基于section)
    5. 在setEditing: 中遍历数组并隐藏/显示每个按钮

    (您可以将按钮存储在另一个数组中以简化操作)

    【讨论】:

    • 我用我改变的内容更新了主帖。根据您的反馈,为什么这不起作用呢?我正在隐藏按钮然后重新加载视图所以它不应该隐藏按钮吗?
    • 您需要更改 viewForHeaderInSection 才能使其正常工作。如果您总是在viewForHeaderInSection 中重新创建按钮,那么将旧按钮设置为隐藏总是会丢失。更改setEditing: 无法解决问题。
    • 如何创建一次标头并返回与您所说的相同的实例? (基于我当前的 viewForHeaderInSection
    猜你喜欢
    • 1970-01-01
    • 2014-04-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-15
    • 2011-11-07
    • 1970-01-01
    相关资源
    最近更新 更多