[英]How to know what a tap has been made on a header section of a UITableView?
我有時會有一個標題部分(只有一個,因為我只有一個部分)。 我添加了一個手勢(單擊),如下所示:
let singleTap = UIShortTapGestureRecognizer(target: self, action: "singleTap:")
singleTap.numberOfTapsRequired = 1
singleTap.numberOfTouchesRequired = 1
tableView.headerViewForSection(0)?.addGestureRecognizer(singleTap)
有用。 問題是,當我的tableView只有幾行時,如果我在最后一行(空白區域)下方點擊,它也會觸發手勢。
怎么避免呢? 我想的方法可能是檢查水龍頭的位置是否在標題的范圍內,但我不知道該怎么做。
對於正常的行,它很容易,我只使用tableView.indexPathForRowAtPoint(point: CGPoint)
但是對於標題呢?
編輯:
當我調用tableView.headerViewForSection(0)
時似乎需要注冊我的頭來獲取一些東西,但問題是我的頭是UIView
,而不是UITableViewHeaderFooterView
或UITableViewCell
所以我無法注冊它(我想......)。
它到目前為止工作,因為我也在桌面視圖上添加了手勢。
因此,當手勢添加到整個tableView時,當我從手勢獲取視圖時,我得到tableView而不是標題。
所以我所做的是使用其標簽從tableView獲取標題:
let header = tableView.viewWithTag(Tag.Header.rawValue)
然后檢查點擊位置是否在標題視圖中:
let location = sender.locationInView(sender.view)
if header!.pointInside(location, withEvent: nil) { ... }
一種可能更好的方法是在viewController的mainView(tableView)上設置gestureRecognizer,並在gestureRecognizer的手勢動作中檢測tapped視圖:
{
let singleTap = UIShortTapGestureRecognizer(target: self, action: "singleTap:")
singleTap.numberOfTapsRequired = 1
singleTap.numberOfTouchesRequired = 1
tableView.addGestureRecognizer(singleTap) // <--
}
...
@IBAction func singleTap(tapGestureRecognizer: UIShortTapGestureRecognizer) {
// detect the tapped view
var loc = tapGestureRecognizer.locationInView(self.tableView)
if (CGRectContainsPoint(tableView.headerViewForSection(0)?.frame, loc)) {
// header was tapped
...
}
}
嘗試為HeaderView添加Tag。
-(void) selectheader:(UITapGestureRecognizer *)gestureRecognizer{
UIView *headerView = gestureRecognizer.view;
if (headerView.tag) {
}
}
和
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 18)];
view.tag = section;
// add your gesture
return view;
}
一個簡單的方法是在表通知你時(通過UITableViewDelegate
)附加監聽器,它將顯示一個標題:
override func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
let singleTap = // setup gesture
// Add gesture to already created header
view.addGestureRecognizer(singleTap)
}
然后你可以設置選擇器功能:
func singleTap(gesture: UITapGestureRecognizer) {
let headerView = gesture.view
// Do something with header view
}
我有同樣的問題,我想出的解決方案是基於該部分在viewForHeaderInSection
方法內的按鈕上創建不同的目標。 我正在使用一個按鈕,但這應該與UITapGestureRecognizer一樣好用
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
// ...
if section == 0 {
cell.button.addTarget(self, action: #selector(firstSectionTapped(_:)), for: .touchUpInside)
} else if section == 1 {
cell.button.addTarget(self, action: #selector(secondSectionTapped(_:)), for: .touchUpInside)
}
// ...
}
然后在ViewController中:
@objc func goalCategoryTapped(_ sender: UITapGestureRecognizer?) {
print("Section One Tapped")
}
@objc func ideaCategoryTapped(_ sender: UITapGestureRecognizer?) {
print("Section Two Tapped")
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.