如何在启用编辑模式时隐藏自定义按钮?

时间:2013-07-01 05:46:04

标签: iphone ios objective-c uitableview uibutton

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

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

--- ---- EDIT

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

1 个答案:

答案 0 :(得分:0)

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

返回标头时,要么创建一次并始终返回相同的实例,那么更改hidden属性应该有效。或者,每次创建标题时检查表isEditing,并确定结果如何。


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

  1. 创建一个属性来保存标题视图 - 数组。
  2. viewDidLoad中创建标题视图(使用当前代码,但转移到其他方法)。
  3. 将标题视图添加到属性(确保首先初始化数组)
  4. 更改viewForHeaderInSection只返回数组中的标题(基于section
  5. setEditing:中遍历数组并隐藏/显示每个按钮
  6. (您可以将按钮存储在另一个数组中,以便轻松实现)