2

UIButtonのヘッダーでこれが機能しない理由がわかりませんUITableView。そこに表示されていますが、タッチが機能していません。

NSLog ステートメントもトリガーされていません。別のビューまたは何かの下にあるように見えるので、それを見ることができますが、押す操作は機能しません。

これを手伝ってくれてありがとう

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

UIView *sectionHeader = [[UILabel alloc] initWithFrame:CGRectNull];
sectionHeader.backgroundColor = [UIColor clearColor];

[sectionHeader addSubview:self.topMapView];

// add map launch button
mapLaunchButton = [UIButton buttonWithType:UIButtonTypeCustom];
[mapLaunchButton addTarget:self
                    action:@selector(mapButtonTouch:)
          forControlEvents:UIControlEventTouchDown];
[mapLaunchButton setTitle:@"ggggggg" forState:UIControlStateNormal];
mapLaunchButton.frame = CGRectMake(0, 0, 300, 90);
mapLaunchButton.backgroundColor = [UIColor redColor];

[sectionHeader addSubview:mapLaunchButton];


  //  [sectionHeader bringSubviewToFront:mapLaunchButton];

    self.tableView.tableHeaderView = sectionHeader;

    return sectionHeader;

}
- (void)mapButtonTouch:(id)sender {

    NSLog(@"map button was touched");
}
4

5 に答える 5

2

に変更UIControlEventTouchDownするとUIControlEventTouchUpInside

于 2013-10-28T19:31:56.037 に答える
1

1 つはsection headerで、もう 1 つはtable headerです。両方に同じビューを割り当てています。これがあなたの問題の原因かどうかはわかりませんが、始めるためのものです。

そのメソッドを実装する代わりに、viewDidLoad でそのビューを作成し、それをself.tableView.tableHeaderView

さらに、最も一般的なユーザー エクスペリエンスは に関連付けられていますUIControlEventTouchUpInside(アクションは、指が離されてボタン内にあるまで実行されません)。ただし、これはおそらく問題とは関係ありません。アクションがいつ呼び出されるかだけの問題です。

これで問題が解決しない場合はお知らせください。他に何か見落としがないか確認します

乾杯!

于 2013-10-28T19:38:27.763 に答える
0

ビューを追加する場合

self.tableView.tableHeaderView = ({
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 184.0f)];

新しいコンポーネントを追加できます。UIImageView, UIlabel... 私にとってはうまくいきます! テストしました。

最終的なコード:

    self.tableView.tableHeaderView = ({
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 184.0f)];
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 40, 100, 100)];
 imageView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
        imageView.image = [UIImage imageNamed:@"avatar.jpg"];
        imageView.layer.masksToBounds = YES;
        imageView.layer.cornerRadius = 50.0;
        imageView.layer.borderColor = [UIColor whiteColor].CGColor;
        imageView.layer.borderWidth = 3.0f;
        imageView.layer.rasterizationScale = [UIScreen mainScreen].scale;
        imageView.layer.shouldRasterize = YES;
        imageView.clipsToBounds = YES;
UIButton  *mapLaunchButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [mapLaunchButton addTarget:self action:@selector(mapButtonTouch:) forControlEvents:UIControlEventTouchDown];
        [mapLaunchButton setTitle:@"ggggggg" forState:UIControlStateNormal];
        mapLaunchButton.frame = CGRectMake(0, 0, 300, 90);
        mapLaunchButton.backgroundColor = [UIColor redColor];
        [view addSubview:imageView];
        [view addSubview:mapLaunchButton];
        view;
 });
于 2015-05-10T08:55:41.650 に答える
0

私も同じ問題を抱えていました。次の行で UIXXXXX を UILabel から UIView に変更すると、うまくいきました。

 UIView *sectionHeader = [[UIXXXXX alloc] initWithFrame:CGRectNull];

そして私は使用しませんでした:

 self.tableView.tableHeaderView = sectionHeader;
于 2014-03-11T18:45:24.327 に答える