ios UITableViewController 为部分选择标题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7750720/
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-30 21:50:56  来源:igfitidea点击:

UITableViewController select header for section

iosuitableviewuikit

提问by rustybeanstalk

I have a UITableViewwith multiple sections. Each section has a section header (a custom view) is there an easy way to detect when someone selects the section header? (Just like didSelectRowAtIndexPath, but for the header?)

我有UITableView多个部分。每个部分都有一个部分标题(自定义视图)是否有一种简单的方法来检测某人何时选择了部分标题?(就像didSelectRowAtIndexPath,但是对于标题?)

采纳答案by rckoenes

No there is no way to do it with the UITableViewDelegate.

不,没有办法用UITableViewDelegate.

What you can do is to add a button the size of the section header view and add it to the view. Set the tag of the button to the section index. Then just add the UIViewControlleras a target for the UIControlEventTouchUpInside.

您可以做的是添加一个与节标题视图大小相同的按钮并将其添加到视图中。将按钮的标签设置为部分索引。然后只需添加UIViewController为 的目标UIControlEventTouchUpInside

Then by looking at the tag of the button you can see which section is clicked.

然后通过查看按钮的标签,您可以看到单击了哪个部分。

回答by bandejapaisa

This isn't radically different than @rckoenes answer, but it does provide a more orthodox way of handling events on views rather than using invisible buttons.

这与@rckoenes 的回答没有根本的不同,但它确实提供了一种更正统的处理视图事件的方式,而不是使用不可见的按钮。

I'd rather add a UITapGestureRecognizer to my header view instead of adding invisible buttons and resizing them:

我宁愿在我的标题视图中添加一个 UITapGestureRecognizer,而不是添加不可见的按钮并调整它们的大小:

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

and then:

进而:

- (void) handleGesture:(UIGestureRecognizer *)gestureRecognizer;

You can use gesture.view to see which was touched. Then do whatever you need to do to find out which header it was (tags, data array lookup... )

您可以使用gesture.view 来查看哪个被触摸了。然后做任何你需要做的事情来找出它是哪个标题(标签,数据数组查找......)

回答by hkdalex

Here is what worked for me in Swift 2:

这是在 Swift 2 中对我有用的内容:

override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let footerView = UITableViewHeaderFooterView()
    footerView.textLabel?.text = "Header Text"
    let tapRecognizer = UITapGestureRecognizer(target: self, action: #selector(handleTap))
    tapRecognizer.delegate = self
    tapRecognizer.numberOfTapsRequired = 1
    tapRecognizer.numberOfTouchesRequired = 1
    footerView.addGestureRecognizer(tapRecognizer)
    return footerView
}

@objc func handleTap(gestureRecognizer: UIGestureRecognizer) {
    print("Tapped")
}

回答by tsuz

This worked for evaluating the section and row. I hope it helps everybody else who is struggling to get this work properly...

这适用于评估部分和行。我希望它可以帮助其他正在努力使这项工作正常进行的人......

override func viewDidLoad() {
    let tapGesture = UITapGestureRecognizer(target: self, action: #selector(sectionTapped(sender:)))
    tableView?.addGestureRecognizer(tapGesture)
    tapGesture.delegate = self as? UIGestureRecognizerDelegate
}

@objc func sectionTapped(sender: UITapGestureRecognizer) {
    if sender.state == UIGestureRecognizerState.ended {
        guard let tableView = self.tableView else {
            return
        }
        if let view = sender.view {
            let tapLocation = sender.location(in: tableView)
            if let tapIndexPath = tableView.indexPathForRow(at: tapLocation) {              
                if (tableView?.cellForRow(at: tapIndexPath) as? UITableViewCell) != nil {
                    // do something with the row
                    print("tapped on row at index: \(tapIndexPath.row)")
                }
            }  else {
                for i in 0..<tableView.numberOfSections {
                    let sectionHeaderArea = tableView.rectForHeader(inSection: i)
                    if sectionHeaderArea.contains(tapLocation) {
                        // do something with the section
                        print("tapped on section at index: \(i)")
                    }
                }
            }
        }
    }
}