0

drawRect: を使用してカスタム ボタンを作成し、テーブルビューのヘッダービューに配置しました。編集モード選択時にカスタムボタンを非表示にしたい。私はメソッドを使用してそれを行うことができることを知っています:

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

1 に答える 1

0

テーブル ビューがリロードされるたびに、新しいボタンの作成を含め、コードによってヘッダー全体が再作成されます。したがって、古いボタンを非表示に設定すると、破棄されて、新しい可視ボタンに置​​き換えられます。

ヘッダーを返すときは、ヘッダーを一度作成して常に同じインスタンスを返すと、hiddenプロパティの変更が機能するはずです。isEditingまたは、ヘッダーが作成されるたびにテーブルを確認し、結果として何をすべきかを決定します。


より効率的なヘッダー再利用オプションを使用しましょう。

  1. ヘッダー ビュー (配列) を保持するプロパティを作成します。
  2. viewDidLoadヘッダー ビューを作成します (現在のコードを使用しますが、別の方法に移動しました) 。
  3. ヘッダー ビューをプロパティに追加します (最初に配列を初期化してください)。
  4. 配列からヘッダーを返すだけに変更viewForHeaderInSectionします (に基づくsection)
  5. setEditing:配列を反復処理し、各ボタンを非表示/表示します

(ボタンを別の配列に保存して、簡単にすることができます)

于 2013-07-01T06:20:00.930 に答える