2013-07-01 71 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

相關問題