UITableView:viewForHeaderInSection,视图无法响应

时间:2015-01-27 01:23:11

标签: ios objective-c uibutton

现在,我遇到了一个新问题:

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来描述.YJHeaderView中有一个UISWitch。问题是当我运行应用程序时,无法触摸UISwitch。

1 个答案:

答案 0 :(得分:1)

您应该以编程方式向该按钮添加一个事件处理程序,如下所示:

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