繁体   English   中英

UITableViewController 为部分选择标题

[英]UITableViewController select header for section

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

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

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

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;

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

不,没有办法用UITableViewDelegate做到这一点。

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

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

这是在 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)")
                    }
                }
            }
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM