UIButton不能在我的UITableView的标题中工作

时间:2013-10-28 19:30:33

标签: ios uitableview uibutton

我无法弄清楚为什么UIButtonUITableView的标题中无效。它出现在那里,但触摸不起作用。

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

5 个答案:

答案 0 :(得分:2)

如果您将UIControlEventTouchDown更改为UIControlEventTouchUpInside

答案 1 :(得分:2)

我可以看到不止一个错误 -

  1. 如果您的表只有一个部分(要验证numberOfSectionsInTableView委托的此检查),请删除此行 -

    self.tableView.tableHeaderView = sectionHeader;

  2. sectionHeader.frame设置为适当的值(不是CGRectNull)。设置节标题(与表标题视图相比)的好处是,当用户将滚动表格行时,节标题将粘在顶部(浮动)并且不会消失。 (普通样式表)

  3. 问题仍未解决,请验证他的方法 -

    - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {    返回50; //取决于sectionHeader的高度 }

  4. 正如其他海报所指出的,在UIButton UIControlEventTouchUpInside上捕捉触摸事件是首选事件。

  5. 编辑 - (作为表标题视图实现)

    如果要向上滚动,请将其设为表格标题(不是章节标题)。因此,从viewForHeaderInSection中删除所有这些并将其放在视图控制器类的viewDidLoad中。保持这一行(在这种情况下不要删除它) -

    self.tableView.tableHeaderView = sectionHeader;
    

    然而,上述第2点和第4点仍然适用。

答案 2 :(得分:1)

有一件事是section header,另一件事是table header。您要为两者分配相同的视图。我不确定这是你问题的原因,但这是让你开始的事情。

不是在viewDidLoad上实现该方法,而是创建该视图并将其指定为self.tableView.tableHeaderView

此外,最常见的用户体验与UIControlEventTouchUpInside相关联(在手指抬起并仍在按钮内部之前,操作不会执行)。但是,这可能与您的问题无关。这只是在何时召唤行动的问题。

如果这不能解决您的问题,请告诉我,我会尝试检查是否还有其他错过

干杯!

答案 3 :(得分:0)

我遇到了同样的问题。在以下行中将UIXXXXX从UILabel更改为UIView对我有用:

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

我没有使用:

 self.tableView.tableHeaderView = sectionHeader;

答案 4 :(得分: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;
 });