2013-07-01 72 views
0

我使用drawRect创建了一个自定义按钮:并将它放在我的headerview中用于我的tableview。我想在选择编辑模式时隐藏自定义按钮。我知道我可以通过使用方法做:启用编辑模式时如何隐藏自定义按钮?

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

但出于某种原因,我的按钮实际上没有当我是1)将其设置为无,2),或使用button.hidden财产dissapearing。这里是我的代码:

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

回答

0

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

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


让我们用更高效的头重用选项:

  1. 创建一个属性来保存头视图(S) - 数组。
  2. viewDidLoad创建标题视图(使用您当前的代码,但移动到不同的方法)。
  3. 添加标题视图属性(确保你初始化数组第一)
  4. 变化viewForHeaderInSection刚刚从阵列(基于section
  5. 通过数组中返回头在setEditing:迭代和隐藏/显示各按钮

(你可以保存按钮另一个阵列,使那么容易)

+0

我更新了主要职位有什么我改变。 根据您的反馈,为什么不工作呢?我正在隐藏按钮,然后重新加载视图,所以不应该隐藏按钮? – EvilAegis

+0

您需要更改'viewForHeaderInSection'才能使其工作。如果您总是在'viewForHeaderInSection'中重新创建按钮,那么将旧按钮设置为隐藏状态总是会丢失。改变'setEditing:'不能解决问题。 – Wain

+0

我如何创建标题一次,并返回相同的实例,如你所说? (基于我当前viewForHeaderInSection – EvilAegis

相关问题