问题
I have made a custom section-header for UITableView, that includes some controls like segmented control, UIImageView ,etc. It successfully appears, but it's not tappable so I can't interact with its controls.
I need to know how can I make this header view selectable?
回答1:
There is no way to do it with the UITableViewDelegate.
You have to add a UITapGestureRecognizer to yourHeaderView Like:
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
FOR Swift 3.0
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(YOURVIEWCONTROLLER.TapGestureRecognizer(_:)))
yourHeaderView.addGestureRecognizer(tapGesture)
func TapGestureRecognizer(gestureRecognizer: UIGestureRecognizer) {
//do your stuff here
}
Swift >= 4.0
Add UIGestureRecognizerDelegate class reference inherited
let tap = UITapGestureRecognizer(target: self, action:#selector(self.handleTap(_:)))
tap.delegate = self
self.view.addGestureRecognizer(tap)
@objc func handleTap(_ sender: UITapGestureRecognizer) {
//Do your work
}
may be it will help.
Happy Coding.
回答2:
Here is what worked for me:
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")
}
回答3:
Simple drop in solution for 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)
}
回答4:
Create a custom section headerView.. and then add Tap gesture to it.
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
}
回答5:
Create a custom view used as section head view and implement the method
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
, it will be called when your finger tap on it.
回答6:
You can simply add a UIButton to your header view (or your header view can be a UIButton itself).
- (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{
}
回答7:
If you need to know the index of the tapped section, you can use the following pattern (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
// ...
}
回答8:
My implementation in Swift:
In your UITableViewController
:
let myView = MyView()
let tapRecognizer = UITapGestureRecognizer(target: myView, action: "handleTap")
myView.addGestureRecognizer(tapRecognizer)
self.tableView.tableHeaderView = myView
In MyView
:
class MyView: UIView {
func handleTap() {
// Handle touch event here
}
}
回答9:
swift solution for this:
let tapR : UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "gotoHeaderArticle")
tapR.delegate = self
tapR.numberOfTapsRequired = 1
tapR.numberOfTouchesRequired = 1
yourView.addGestureRecognizer(tapR)
Then implement
func gotoHeaderArticle() {
}
Make sure to make your calling viewcontroller adhere to UIGestureRecognizerDelegate
回答10:
Update for 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")
}
回答11:
Here is what worked for me
-(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);
}
回答12:
If anyone needs a solution that can work when the tableview has multiple sections, I had the same problem and the solution I came up with was to create different targets on a button at the viewForHeaderInSection
method, based on the section. I am using a button but this should work just as well with 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)
}
// ...
}
Then in the ViewController:
@objc func goalCategoryTapped(_ sender: UITapGestureRecognizer?) {
print("Section One Tapped")
}
@objc func ideaCategoryTapped(_ sender: UITapGestureRecognizer?) {
print("Section Two Tapped")
}
来源:https://stackoverflow.com/questions/19996566/how-to-make-uitableview-header-selectable