Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/100.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios UITableViewController为节选择标题_Ios_Uitableview_Uikit - Fatal编程技术网

Ios UITableViewController为节选择标题

Ios UITableViewController为节选择标题,ios,uitableview,uikit,Ios,Uitableview,Uikit,我有一个包含多个部分的UITableView。每个部分都有一个部分标题(自定义视图)。是否有一种简单的方法可以检测何时有人选择了部分标题?(就像didselectrowtaindexpath,但对于标题?否使用UITableViewDelegate无法执行此操作 您可以做的是添加一个与节标题视图大小相同的按钮,并将其添加到视图中。将按钮的标记设置为节索引。 然后只需添加UIViewController作为UIControlEventTouchUpInside的目标 然后通过查看按钮的标签,您可以

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

否使用
UITableViewDelegate
无法执行此操作

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


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

这与@rckoenes答案没有根本区别,但它确实提供了一种更传统的方式来处理视图上的事件,而不是使用不可见的按钮

我宁愿将UITapGestureRecognitor添加到标题视图,而不是添加不可见的按钮并调整其大小:

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

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

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

以下是我在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")
}

这有助于评估剖面和行。我希望它能帮助其他正在努力使这项工作正常进行的人

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)")
                    }
                }
            }
        }
    }
}
override func viewDidLoad(){
让Tap手势=UITapGestureRecognizer(目标:自我,操作:#选择器(分段点击(发送方:))
tableView?.AddGestureRecognitor(点击手势)
tappostate.delegate=self-as?UIgestureRecognitizerDelegate
}
@objc func sectionTapped(发送方:UITAPGESTURE识别器){
如果sender.state==UIGestureRecognitzerState.ended{
guard let tableView=self.tableView-else{
返回
}
如果let view=sender.view{
让tapLocation=sender.location(在:tableView中)
如果让tapIndexPath=tableView.indexPathForRow(at:tapLocation){
if(tableView?.cellForRow(at:tapIndexPath)as?UITableViewCell)!=nil{
//对这排人做点什么
打印(“点击索引处的行:\(tapIndexPath.row)”)
}
}否则{

对于0中的i..我认为您必须将方法更改为:
-(void)gestureHandler:(uigestureerecognizer*)gestureerecognizer;
,因为您将操作定义为
gestureHandler
使用YourView*tappedView=(YourView*)gestureRecognizer.view;要查看触摸了哪个视图更明确,所需的
numberOfTouchesRequired
numberOfTapsRequired
默认为1,无需将它们设置为1,或者只需在“viewForHeaderInSection”方法中返回标题大小的UIButton,而无需将其添加为视图并返回