我有一个带有多个分区的UITableView
。 每个分区都有一个分区头部(自定义视图),是否有一种简单的方法来检测当有人选择了该分区的头部?(就像didSelectRowAtIndexPath
,但是用于头部?)
我有一个带有多个分区的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)")
}
}
}
}
}
}
tapIndexPath.row
的值为0
。如果你的目标是在节标题上进行点击,则可以省略if let view = sender.view
块。 - J Kasparian这是Taku答案的稍微更新版本。
override func viewDidLoad() {
super.viewDidLoad()
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(sectionTapped(sender:)))
tableView.addGestureRecognizer(tapGesture)
tapGesture.delegate = self
}
@objc func sectionTapped(sender: UITapGestureRecognizer) {
guard sender.state == .ended else { return }
let tapLocation = sender.location(in: tableView)
if let tapIndexPath = tableView.indexPathForRow(at: tapLocation),
let cell = tableView.cellForRow(at: tapIndexPath) {
// TODO: Do something with row
print("tapped on row at index: \(tapIndexPath.row)")
} else {
for section in 0..< tableView.numberOfSections {
let headerRect = tableView.rectForHeader(inSection: section)
if headerRect.contains(tapLocation) {
// TODO: Do something with the section
print("Tapped on section at index: \(section)")
}
}
}
}
- (void) gestureHandler:(UIGestureRecognizer *)gestureRecognizer;
因为你定义动作为gestureHandler
。 - fsulsernumberOfTouchesRequired
和numberOfTapsRequired
默认值为 1,无需将它们设置为 1。 - Jonathan Cabrera