-1

次のコードを使用して配列がnilでない場合はボタンを非表示にしたいのですが、何らかの理由でボタンが機能していません。コードに間違いがありません。助けてください。ありがとうございます。

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    NSString *title = nil;

     HowtoUseButton = [[[CustomButton alloc] init] autorelease];

    [HowtoUseButton addTarget:self action:@selector(openLink) forControlEvents:UIControlEventTouchDown];

    HowtoUseButton.frame = CGRectMake(80.0, 140.0, 160.0, 70.0);


    [self.view addSubview:HowtoUseButton];


    if (document.count > 0){

        HowtoUseButton.hidden = YES;  // not working
        title = Title_Doc;

    }
    else if (self.documentURLs.count == 0){

        title = Title_No_Doc;
        HowtoUseButton.hidden = NO;

    }

    return title;
}
4

3 に答える 3

1

でこれを使用しているのでtitleForHeaderInSection、おそらく複数回呼び出されています。その場合、おそらく一部のボタンだけが非表示になっています。

変更してみてください:

HowtoUseButton = [[[CustomButton alloc] init] autorelease];
[HowtoUseButton addTarget:self action:@selector(openLink) forControlEvents:UIControlEventTouchDown];
HowtoUseButton.frame = CGRectMake(80.0, 140.0, 160.0, 70.0);
[self.view addSubview:HowtoUseButton];

に:

if (!HowtoUseButton) {
    HowtoUseButton = [[[CustomButton alloc] init] autorelease];
    [HowtoUseButton addTarget:self action:@selector(openLink) forControlEvents:UIControlEventTouchDown];
    HowtoUseButton.frame = CGRectMake(80.0, 140.0, 160.0, 70.0);
    [self.view addSubview:HowtoUseButton];
}

また、すでに答えた他の人々が言っ​​たように、これはそもそもここにあるべきではありません。インターフェイスビルダーで、または一度にボタンを作成し、viewDidLoad変更する必要がある場所に非表示のプロパティを設定するだけです。

于 2012-11-28T00:23:08.480 に答える
0

プロパティを使用する代わりにhidden、ビューからプロパティを削除してみませんか?

[HowtoUseButton removeFromSuperview];

余談ですが、ここで行っていることは少し奇妙です。テーブルビューのセクションヘッダーにボタンが必要な場合、これを実行する最善の方法は、おそらく次のことを実装することです。

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section

...テーブルビューデリゲート内。これにより、テーブルヘッダービューを完全にカスタマイズできます。

于 2012-11-28T00:09:56.533 に答える
0

titleForHeaderこのコードをviewDidLoadクラスのメソッドに保持するメソッドからボタン作成部分を削除する必要があります。

- (void)viewDidLoad {
   [super viewDidLoad];
   HowtoUseButton = [[[CustomButton alloc] init] autorelease];
   [HowtoUseButton addTarget:self action:@selector(openLink) forControlEvents:UIControlEventTouchDown];
   HowtoUseButton.frame = CGRectMake(80.0, 140.0, 160.0, 70.0);
   [self.view addSubview:HowtoUseButton];

これをtitleForHeaderメソッドで作成しているため、複数回作成され、新しいものが作成されると、古いものへの参照が失われます。titleForHeaderテーブルビューをスクロールするたびにメソッドが呼び出されます。viewDidLoad代わりに、メソッドで一度作成して使用することができます。

また、このボタンをテーブルビューヘッダーに追加する場合はviewForHeaderInSection、他の回答に記載されているように使用してください。

于 2012-11-28T00:22:03.967 に答える