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
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")
}
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{
}
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.
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.
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")
}
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);
}