22

我为 UITableView 制作了一个自定义节标题,其中包括一些控件,如分段控件、UIImageView 等。它成功出现,但它不可点击,因此我无法与其控件交互。

我需要知道如何使这个标题视图可选?

4

12 回答 12

30

UITableViewDelegate无法做到这一点。

你必须添加一个 UITapGestureRecognizer 到 yourHeaderView 像:

UITapGestureRecognizer *singleTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(gestureHandler:)];
[singleTapRecognizer setDelegate:self];
singleTapRecognizer.numberOfTouchesRequired = 1;
singleTapRecognizer.numberOfTapsRequired = 1;   
[yourHeaderView addGestureRecognizer:singleTapRecognizer];


-(void) handleGesture:(UIGestureRecognizer *)gestureRecognizer; //do in this method whatever you want

对于 Swift 3.0

let tapGesture = UITapGestureRecognizer(target: self, action: #selector(YOURVIEWCONTROLLER.TapGestureRecognizer(_:)))
yourHeaderView.addGestureRecognizer(tapGesture)

func TapGestureRecognizer(gestureRecognizer: UIGestureRecognizer) {
    //do your stuff here 
}

斯威夫特 >= 4.0

添加UIGestureRecognizerDelegate 类引用继承

let tap = UITapGestureRecognizer(target: self, action:#selector(self.handleTap(_:)))
tap.delegate = self
self.view.addGestureRecognizer(tap)

  @objc func handleTap(_ sender: UITapGestureRecognizer) {
        //Do your work 
    }

可能会有所帮助。

快乐编码。

于 2013-11-15T10:57:46.780 回答
7

这对我有用:

override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {

    let v = UITableViewHeaderFooterView()
    v.textLabel?.text = "Header Text"
    let tapRecognizer = UITapGestureRecognizer(target: self, action: #selector(handleTap))
    tapRecognizer.delegate = self
    tapRecognizer.numberOfTapsRequired = 1
    tapRecognizer.numberOfTouchesRequired = 1
    v.addGestureRecognizer(tapRecognizer)
    return v
}

func handleTap(gestureRecognizer: UIGestureRecognizer)
{
    print("Tapped")
}
于 2016-05-24T16:27:39.513 回答
7

如果您需要知道抽头部分的索引,可以使用以下模式(Swift 4):

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let view = UIView(frame: CGRect(x: 0, y: 0, width: 300, height: 44))

    // Configure your view here
    // ...

    let tapGestureRecognizer = UITapGestureRecognizer(
        target: self,
        action: #selector(headerTapped(_:))
    )
    view.tag = section
    view.addGestureRecognizer(tapGestureRecognizer)
    return view
}

@objc func headerTapped(_ sender: UITapGestureRecognizer?) {
    guard let section = sender?.view?.tag else { return }

    // Use your section index here
    // ...
}
于 2019-08-21T07:26:17.717 回答
4

Swift 3.0 的简单解决方案

 func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {

        let v = UITableViewHeaderFooterView()
        let tapRecognizer = UITapGestureRecognizer(target: self, action: #selector(handleTap))
        v.addGestureRecognizer(tapRecognizer)
        return v
    }


    func handleTap(gestureRecognizer: UIGestureRecognizer)
    {
        controller?.view.endEditing(true)
    }
于 2016-11-10T07:16:58.273 回答
3

创建一个自定义部分 headerView.. 然后向其添加 Tap 手势。

UITapGestureRecognizer * recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];

//This method will be called on Tap of section header
- (void)handleTap:(UITapGestureRecognizer *)sender {
//Do the action on Tap
}
于 2013-11-15T09:24:42.417 回答
3

您可以简单地将 UIButton 添加到您的标题视图(或者您的标题视图可以是 UIButton 本身)。

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

        static NSString *cellIdentifier = @"cellIdentifier";

    UITableViewCell *cell = (UITableViewCell*)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    UIButton *cellButton = (UIButton*)[cell viewWithTag:1];
    [cellButton addTarget:self action:@selector(premiumSectionClicked:) forControlEvents:UIControlEventTouchUpInside];

    return cell;
}

-(void)buttonTapped:(id)sender{

}
于 2015-09-22T07:34:52.937 回答
2

创建一个用作截面头视图的自定义视图并实现该方法 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event,当您的手指点击它时将调用它。

于 2013-11-15T09:15:11.173 回答
1

我在 Swift 中的实现:

在你的UITableViewController

let myView = MyView()

let tapRecognizer = UITapGestureRecognizer(target: myView, action: "handleTap")
myView.addGestureRecognizer(tapRecognizer)

self.tableView.tableHeaderView = myView

MyView

class MyView: UIView {

    func handleTap() {
        // Handle touch event here
    }
}
于 2015-08-14T16:38:56.523 回答
1

swift solution for this:

let tapR : UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "gotoHeaderArticle")
tapR.delegate = self
tapR.numberOfTapsRequired = 1
tapR.numberOfTouchesRequired = 1
yourView.addGestureRecognizer(tapR)

Then implement

func gotoHeaderArticle() {

}

Make sure to make your calling viewcontroller adhere to UIGestureRecognizerDelegate

于 2015-10-20T02:46:42.200 回答
0

如果有人需要在 tableview 有多个部分时可以工作的解决方案,我遇到了同样的问题,我想出的解决方案是viewForHeaderInSection根据部分在方法的按钮上创建不同的目标。我正在使用一个按钮,但这应该与 UITapGestureRecognizer 一样好用

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {

        // ...

        if section == 0 {
            cell.button.addTarget(self, action: #selector(firstSectionTapped(_:)), for: .touchUpInside)
        } else if section == 1 {
            cell.button.addTarget(self, action: #selector(secondSectionTapped(_:)), for: .touchUpInside)
        }

        // ...

}

然后在 ViewController 中:

@objc func goalCategoryTapped(_ sender: UITapGestureRecognizer?) {
    print("Section One Tapped")
}

@objc func ideaCategoryTapped(_ sender: UITapGestureRecognizer?) {
    print("Section Two Tapped")
}
于 2019-07-15T06:27:16.477 回答
0

Swift 3.1 更新

let tapGesture = UITapGestureRecognizer(target: self, action: #selector(TapGestureRecognizer(gestureRecognizer:)))
yourView.addGestureRecognizer(tapGesture)

func TapGestureRecognizer(gestureRecognizer: UIGestureRecognizer) {
        //do your stuff here
        print("Gesture")
    }
于 2017-08-04T08:05:17.900 回答
0

这对我有用

  -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
        static NSString *cellIdentifier = @"cellIdentifier";
    YourHeaderTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
   cell.imageView.image = [image imageNamed:@"imageName"];
cell.segmentedControl.tag = section;
[cell.segmentedControl addTarget:self
                     action:@selector(action:)
           forControlEvents:UIControlEventValueChanged];
    return cell;
}

- (void)action:(id)sender{
    UISegmentedControl *segmentedControl = (UISegmentedControl *) sender;
    NSLog(@"selected section:%ld",(long)segmentedControl.tag);

}
于 2017-09-05T08:27:53.483 回答