UITableView:viewForHeaderInSection,视图无法响应

UITableView:viewForHeaderInSection, the view can't response

现在,我有一个新问题:

YJHomeViewContoller.m

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
return [YJHeaderView view];
}

YJHeadView.m:

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
YJHeaderView *view = [[[NSBundle mainBundle] loadNibNamed:@"YJHeaderView" owner:nil options:nil] lastObject];
}

YJHeaderView 是我的自定义视图,我用 xib 来 describe.There 是一个 UISwitch 在 YJHeaderView.The 问题是当我 运行 应用程序时,无法触摸 UISwitch。

您应该像这样以编程方式向按钮添加事件处理程序:

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
   UIButton *btn = [UIButton buttonWithType:UIButtonTypeContactAdd];
   btn.tag = section; // Needed to find out which header was tapped
   [btn addTarget:self action:@selector(btnTapped:) forControlEvents:UIControlEventTouchUpInside];
   return btn;
}

当然,然后在同一个 ViewController 中定义 btnTapped: 方法,如下所示:

-(void)btnTapped:(UIButton *)sender {
   int sectionNo = sender.tag;
   // Do something 
}