我为 UITableView 制作了一个自定义节标题,其中包括一些控件,如分段控件、 UIImageView 等。它成功出现,但它不可点击,因此我无法与其控件交互。
我需要知道如何使此标题视图可选择?
无法使用 UITableViewDelegate 来做到这一点。
您必须将 UITapGestureRecognizer 添加到您的 HeaderView 中,例如:
UITapGestureRecognizer *singleTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(gestureHandler:)];
[singleTapRecognizer setDelegate:self];
singleTapRecognizer.numberOfTouchesRequired = 1;
singleTapRecognizer.numberOfTapsRequired = 1;
[yourHeaderView addGestureRecognizer:singleTapRecognizer];
-(void) handleGesture:(UIGestureRecognizer *)gestureRecognizer; //do in this method whatever you want
适用于 Swift 3.0
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(YOURVIEWCONTROLLER.TapGestureRecognizer(_:)))
yourHeaderView.addGestureRecognizer(tapGesture)
func TapGestureRecognizer(gestureRecognizer: UIGestureRecognizer) {
//do your stuff here
}
斯威夫特 >= 4.0
添加UIGestureRecognizerDelegate类引用继承
let tap = UITapGestureRecognizer(target: self, action:#selector(self.handleTap(_:)))
tap.delegate = self
self.view.addGestureRecognizer(tap)
@objc func handleTap(_ sender: UITapGestureRecognizer) {
//Do your work
}
也许会有帮助。
快乐编码。
这对我有用:
override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let v = UITableViewHeaderFooterView()
v.textLabel?.text = "Header Text"
let tapRecognizer = UITapGestureRecognizer(target: self, action: #selector(handleTap))
tapRecognizer.delegate = self
tapRecognizer.numberOfTapsRequired = 1
tapRecognizer.numberOfTouchesRequired = 1
v.addGestureRecognizer(tapRecognizer)
return v
}
func handleTap(gestureRecognizer: UIGestureRecognizer)
{
print("Tapped")
}
如果您需要知道点击部分的索引,您可以使用以下模式(Swift 4):
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let view = UIView(frame: CGRect(x: 0, y: 0, width: 300, height: 44))
// Configure your view here
// ...
let tapGestureRecognizer = UITapGestureRecognizer(
target: self,
action: #selector(headerTapped(_:))
)
view.tag = section
view.addGestureRecognizer(tapGestureRecognizer)
return view
}
@objc func headerTapped(_ sender: UITapGestureRecognizer?) {
guard let section = sender?.view?.tag else { return }
// Use your section index here
// ...
}
Swift 3.0 的简单解决方案
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let v = UITableViewHeaderFooterView()
let tapRecognizer = UITapGestureRecognizer(target: self, action: #selector(handleTap))
v.addGestureRecognizer(tapRecognizer)
return v
}
func handleTap(gestureRecognizer: UIGestureRecognizer)
{
controller?.view.endEditing(true)
}
创建一个自定义部分 headerView..,然后向其添加点击手势。
UITapGestureRecognizer * recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
//This method will be called on Tap of section header
- (void)handleTap:(UITapGestureRecognizer *)sender {
//Do the action on Tap
}
您可以简单地将 UIButton 添加到标题视图(或者标题视图本身可以是 UIButton)。
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
static NSString *cellIdentifier = @"cellIdentifier";
UITableViewCell *cell = (UITableViewCell*)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
UIButton *cellButton = (UIButton*)[cell viewWithTag:1];
[cellButton addTarget:self action:@selector(premiumSectionClicked:) forControlEvents:UIControlEventTouchUpInside];
return cell;
}
-(void)buttonTapped:(id)sender{
}
创建一个自定义视图用作节头视图并实现该方法
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
,手指点击就会调用。
我在 Swift 中的实现:
在你的
UITableViewController
:
let myView = MyView()
let tapRecognizer = UITapGestureRecognizer(target: myView, action: "handleTap")
myView.addGestureRecognizer(tapRecognizer)
self.tableView.tableHeaderView = myView
在
MyView
:
class MyView: UIView {
func handleTap() {
// Handle touch event here
}
}
对此的快速解决方案:
let tapR : UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "gotoHeaderArticle")
tapR.delegate = self
tapR.numberOfTapsRequired = 1
tapR.numberOfTouchesRequired = 1
yourView.addGestureRecognizer(tapR)
然后实施
func gotoHeaderArticle() {
}
确保您的调用视图控制器遵守 UIGestureRecognizerDelegate
更新 Swift 3.1
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(TapGestureRecognizer(gestureRecognizer:)))
yourView.addGestureRecognizer(tapGesture)
func TapGestureRecognizer(gestureRecognizer: UIGestureRecognizer) {
//do your stuff here
print("Gesture")
}
这对我有用
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
static NSString *cellIdentifier = @"cellIdentifier";
YourHeaderTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
cell.imageView.image = [image imageNamed:@"imageName"];
cell.segmentedControl.tag = section;
[cell.segmentedControl addTarget:self
action:@selector(action:)
forControlEvents:UIControlEventValueChanged];
return cell;
}
- (void)action:(id)sender{
UISegmentedControl *segmentedControl = (UISegmentedControl *) sender;
NSLog(@"selected section:%ld",(long)segmentedControl.tag);
}
如果有人需要一个可以在表格视图有多个部分时工作的解决方案,我也遇到了同样的问题,我想出的解决方案是根据部分在
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")
}
根据 hkdalex 的回答,这个解决方案对我有用。
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let sectionHeaderBackgroundColor = UIColor(hue: 0.921, saturation: 0.34, brightness: 0.94, alpha: 0.4)
let sectionHeaderLabelView = UIView()
sectionHeaderLabelView.backgroundColor = sectionHeaderBackgroundColor
let sectionHeaderImage = UIImage(named: "alimentation")
let sectionHeaderImageView = UIImageView(image: sectionHeaderImage)
sectionHeaderImageView.frame = CGRect(x: 3, y: 10, width: 30, height: 30)
sectionHeaderLabelView.addSubview(sectionHeaderImageView)
let sectionHeaderLabel = UILabel()
sectionHeaderLabel.text = self.alimCategories[section].nom//sectionHeaderTitles[section]
sectionHeaderLabel.textColor = .brown
sectionHeaderLabel.font = UIFont.boldSystemFont(ofSize: 20.0)
sectionHeaderLabel.frame = CGRect(x: 43, y: 5, width: 250, height: 40)
sectionHeaderLabelView.addSubview(sectionHeaderLabel)
let tapRecognizer = UITapGestureRecognizer(target: self, action: #selector(sectionHeaderTapped))
tapRecognizer.delegate = self
tapRecognizer.numberOfTapsRequired = 1
tapRecognizer.numberOfTouchesRequired = 1
self.selectedHeaderName = self.alimCategories[section].nom
sectionHeaderLabelView.addGestureRecognizer(tapRecognizer)
return sectionHeaderLabelView
}
@objc func sectionHeaderTapped(gestureRecognizer: UIGestureRecognizer)
{
let headerText = self.selectedHeaderName
print(headerText! + " tapped")
}
使用此代码,您可以使用标题图像和文本自定义节标题,附加点击侦听器并从全局变量获取选定的文本。