UITableview 中的 UITapGesture header
UITapGesture in UITableview header
我试图在 swift 中使用可扩展的表格视图。所以我在 tableview 中将 Gesture 添加到 header,但它无法识别。请指导我。只需交叉检查我的编码。
我的编码如下:
func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let vvv = tableView.headerViewForSection(section)
let tap = UITapGestureRecognizer(target: self, action: Selector("handleTap:"))
tap.delegate = self
vvv?.addGestureRecognizer(tap)
return vvv
}
func handleTap(gestureRecognizer: UITapGestureRecognizer)
{
if(gestureRecognizer.state == .Ended)
{
println("SECTION PRESSED") //NOT ENTERING TO THIS BLOCK
}
}
我是这样做的。
首先,创建一个带有 "headerCellSection" 变量的 UITableViewCell class(或者任何你想称呼它的东西)。
import UIKit
class HeaderCellTableViewCell: UITableViewCell {
var headerCellSection:Int?
override func awakeFromNib() {
super.awakeFromNib()
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
}
将单元格添加到 "viewForHeaderInSection":
func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerCell = tableView.dequeueReusableCellWithIdentifier("headerCell") as! HeaderCellTableViewCell
return headerCell
}
将单元格的部分发送到先前声明的变量并将点击手势识别器添加到单元格。代码应如下所示:
func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerCell = tableView.dequeueReusableCellWithIdentifier("headerCell") as! HeaderCellTableViewCell
// Send section
headerCell.headerCellSection = section
// Add gesture
let headerTapGesture = UITapGestureRecognizer()
headerTapGesture.addTarget(self, action: "myAction:")
headerCell.addGestureRecognizer(headerTapGesture)
return headerCell
}
然后添加动作。在操作中,您可以访问具有点击手势目标的视图。在那里你可以访问 "headerCellSection" 价值和瞧!
func myAction (sender: UITapGestureRecognizer) {
// Get the view
let senderView = sender.view as! HeaderCellTableViewCell
// Get the section
let section = senderView.headerCellSection
print(section)
}
在 Swift 4 中,您可以将 UITapGestureRecognizer 添加到 header 视图:
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerView = UIView()
let tapRecognizer = UITapGestureRecognizer(target: self, action: #selector(headerTap))
headerView.addGestureRecognizer(tapRecognizer)
return headerView
}
@objc func headerTap() {
// Tap action
}
我是通过将 UITapGestureRecognizer
添加到 headerView 的 contentView
来实现的。示例:
headerCell.contentView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(headerTapped)))
我知道回答这个问题有点老了。但是 none 接受的答案,我会给出另一个简单的解决方案。
阅读原问题后,我认为作者不想要自定义创建的部分header。这就是他尝试使用以下代码的原因。
let vvv = tableView.headerViewForSection(section)
但我相信您会在上面的代码片段中得到 vvv 的空值。如果用户想要访问默认视图,他可以按如下方式进行。
tableView.register(UITableViewHeaderFooterView.self, forHeaderFooterViewReuseIdentifier: sectionHeaderIdentifier)
以上注册,我在我的 viewDidLoad() 函数中做了。然后使用下面的代码
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let sectionView: UITableViewHeaderFooterView = tableView.dequeueReusableHeaderFooterView(withIdentifier: sectionHeaderIdentifier)!
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(sectionHeaderTapped))
sectionView.addGestureRecognizer(tapGesture)
return sectionView
}
以上函数将为您的 table 部分 header 创建默认视图,并启用点按手势属性。
我试图在 swift 中使用可扩展的表格视图。所以我在 tableview 中将 Gesture 添加到 header,但它无法识别。请指导我。只需交叉检查我的编码。
我的编码如下:
func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let vvv = tableView.headerViewForSection(section)
let tap = UITapGestureRecognizer(target: self, action: Selector("handleTap:"))
tap.delegate = self
vvv?.addGestureRecognizer(tap)
return vvv
}
func handleTap(gestureRecognizer: UITapGestureRecognizer)
{
if(gestureRecognizer.state == .Ended)
{
println("SECTION PRESSED") //NOT ENTERING TO THIS BLOCK
}
}
我是这样做的。
首先,创建一个带有 "headerCellSection" 变量的 UITableViewCell class(或者任何你想称呼它的东西)。
import UIKit
class HeaderCellTableViewCell: UITableViewCell {
var headerCellSection:Int?
override func awakeFromNib() {
super.awakeFromNib()
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
}
将单元格添加到 "viewForHeaderInSection":
func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerCell = tableView.dequeueReusableCellWithIdentifier("headerCell") as! HeaderCellTableViewCell
return headerCell
}
将单元格的部分发送到先前声明的变量并将点击手势识别器添加到单元格。代码应如下所示:
func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerCell = tableView.dequeueReusableCellWithIdentifier("headerCell") as! HeaderCellTableViewCell
// Send section
headerCell.headerCellSection = section
// Add gesture
let headerTapGesture = UITapGestureRecognizer()
headerTapGesture.addTarget(self, action: "myAction:")
headerCell.addGestureRecognizer(headerTapGesture)
return headerCell
}
然后添加动作。在操作中,您可以访问具有点击手势目标的视图。在那里你可以访问 "headerCellSection" 价值和瞧!
func myAction (sender: UITapGestureRecognizer) {
// Get the view
let senderView = sender.view as! HeaderCellTableViewCell
// Get the section
let section = senderView.headerCellSection
print(section)
}
在 Swift 4 中,您可以将 UITapGestureRecognizer 添加到 header 视图:
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerView = UIView()
let tapRecognizer = UITapGestureRecognizer(target: self, action: #selector(headerTap))
headerView.addGestureRecognizer(tapRecognizer)
return headerView
}
@objc func headerTap() {
// Tap action
}
我是通过将 UITapGestureRecognizer
添加到 headerView 的 contentView
来实现的。示例:
headerCell.contentView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(headerTapped)))
我知道回答这个问题有点老了。但是 none 接受的答案,我会给出另一个简单的解决方案。
阅读原问题后,我认为作者不想要自定义创建的部分header。这就是他尝试使用以下代码的原因。
let vvv = tableView.headerViewForSection(section)
但我相信您会在上面的代码片段中得到 vvv 的空值。如果用户想要访问默认视图,他可以按如下方式进行。
tableView.register(UITableViewHeaderFooterView.self, forHeaderFooterViewReuseIdentifier: sectionHeaderIdentifier)
以上注册,我在我的 viewDidLoad() 函数中做了。然后使用下面的代码
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let sectionView: UITableViewHeaderFooterView = tableView.dequeueReusableHeaderFooterView(withIdentifier: sectionHeaderIdentifier)!
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(sectionHeaderTapped))
sectionView.addGestureRecognizer(tapGesture)
return sectionView
}
以上函数将为您的 table 部分 header 创建默认视图,并启用点按手势属性。