如何使UITableView标题可选?
我为UITableView制作了一个自定义的部分头,包括一些控件,如分段控件,UIImageView等等。 它成功地出现了,但是它不能点击,所以我不能和它的控件进行交互。
我需要知道如何使这个标题视图可选?
有没有办法与UITableViewDelegate做到这一点。
你必须添加一个UITapGestureRecognizer到yourHeaderView像:
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 }
可能会有帮助。
快乐的编码。
创build一个自定义的部分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 }
这是什么对我有用:
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 3.0简单的解决scheme
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) }
你可以简单地添加一个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{ }
创build一个自定义的视图,用作区域头部视图并实现该方法- (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() { }
确保你的调用viewcontroller坚持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); }