How to make UITableView Header selectable?

后端 未结 12 1283
执笔经年
执笔经年 2020-12-16 11:49

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

相关标签:
12条回答
  • 2020-12-16 12:31

    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")
    }
    
    0 讨论(0)
  • 2020-12-16 12:38

    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{
    
    }
    
    0 讨论(0)
  • 2020-12-16 12:38

    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.

    0 讨论(0)
  • 2020-12-16 12:39

    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.

    0 讨论(0)
  • 2020-12-16 12:41

    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")
        }
    
    0 讨论(0)
  • 2020-12-16 12:42

    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);
    
    }
    
    0 讨论(0)
提交回复
热议问题