ios 如何使 UITableView Header 可选?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19996566/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 02:56:20  来源:igfitidea点击:

How to make UITableView Header selectable?

iosuitableview

提问by kaymas

I have made a custom section-header for UITableView, that includes some controls like segmented control, UIImageView ,etc. It successfully appears, but it's not tappable so I can't interact with its controls.

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

I need to know how can I make this header view selectable?

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

回答by Dhaval Bhadania

There is no way to do it with the UITableViewDelegate.

UITableViewDelegate没有办法做到这一点

You have to add a UITapGestureRecognizer to yourHeaderView Like:

您必须将 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

FOR Swift 3.0

适用于 Swift 3.0

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

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

Swift >= 4.0

斯威夫特 >= 4.0

Add UIGestureRecognizerDelegateclass reference inherited

添加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 
    }

may be it will help.

可能会有所帮助。

Happy Coding.

快乐编码。

回答by hkdalex

Here is what worked for me:

这是对我有用的:

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

回答by netshark1000

Simple drop in solution for Swift 3.0

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

回答by naveed148

You can simply add a UIButton to your header view (or your header view can be a UIButton itself).

您可以简单地将 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{

}

回答by dRAGONAIR

Create a custom section headerView.. and then add Tap gesture to it.

创建一个自定义部分 headerView.. 然后向它​​添加点击手势。

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
}

回答by dulgan

If you need to know the index of the tapped section, you can use the following pattern (Swift 4) :

如果您需要知道点击部分的索引,您可以使用以下模式(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
    // ...
}

回答by KudoCC

Create a custom view used as section head view and implement the method - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event, it will be called when your finger tap on it.

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

回答by JAL

My implementation in Swift:

我在 Swift 中的实现:

In your UITableViewController:

在您的UITableViewController

let myView = MyView()

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

self.tableView.tableHeaderView = myView

In MyView:

MyView

class MyView: UIView {

    func handleTap() {
        // Handle touch event here
    }
}

回答by Apple-and-Oranges

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

确保让你的调用视图控制器遵守 UIGestureRecognizerDelegate

回答by aqsa arshad

Update for Swift 3.1

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