Double Tap Necessary to Select Tableview Item with Search Bar

Double tap necessary to select TableView item with Search Bar

You can add a tap gesture recognizer to your view that when fired would

  1. resign the search bar responder
  2. convert the tap location to a point in you table view
  3. get the cell that tap location
  4. manually call didSelectRowAt which isn't the "best" solution but for demonstration purposes.

Here is how it looks implemented:

override func viewDidLoad() {
super.viewDidLoad()

// Add a tap gesture to our view
let gesture = UITapGestureRecognizer(target: self, action: #selector(TestTableViewController.didTap(_:)))
self.view.addGestureRecognizer(gesture)
}

// Called when the tap gesture fires
func didTap(gesture: UITapGestureRecognizer) {

// We get rid of our keyboard on screen
searchBar.resignFirstResponder()

// Find the location of the touch relative to the tableView
let touch = gesture.locationInView(self.tableView)

// Convert that touch point to an index path
if let indexPath = tableView.indexPathForRowAtPoint(touch) {

// Here I am just calling the delegate method directly which you shouldn't do.
// You should just do whatever you want to do with the indexPath here.
tableView(tableView, didSelectRowAtIndexPath: indexPath)
}
}

Tap on empty UITableView to resign search bar

You'll have to create your own UITableView class that overrides the touchesEnded method. Then you can call back to your delegate, which should be your view controller, and tell it to tell your UISearchBar to resign first responder.

The UITableView derived class would look something like this:

#import <UIKit/UIKit.h>

@interface FRTableView : UITableView {
}

@end

// ------------------------------

#import "FRTableView.h"

@implementation FRTableView

- (id)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
// Initialization code
}
return self;
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesEnded:touches withEvent:event];
NSLog(@"Touches ended");

// Bubble back up to your view controller which should be this
// table view's delegate.
if ([self delegate] && [[self delegate] respondsToSelector:@selector(touchEnded)])
[[self delegate] performSelector:@selector(touchEnded)];
}

@end

Make sure you set your table view in Interface Builder to use your custom class instead of the normal UITableView class. Then in your view controller, implement the selector I've called touchEnded like this:

- (void)touchEnded;
{
NSLog(@"Back in root view controller.");
[searchBar resignFirstResponder];
}

Where searchBar is an IBOutlet connected to a UISearchBar in Interface Builder.

changing frame of tableView to show and hide a search bar

got it working using the table header view. I needed to make my search bar strong so that the view didnt get released when it was set to nil.

@property (strong, nonatomic) IBOutlet UISearchBar *searchBar;

then to show and hide it just set the header to nil to hide or assign it the searchBar to show it

 //hide the search bar
self.tableView.tableHeaderView = nil;

//show the search bar
self.tableView.tableHeaderView = self.searchBar;

UISearchBar gesture recognizer immediately selects cell in tableview

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
if([self.view isFirstResponder])
{
[self.view endEditing:YES];
cell.userInteractionEnabled = NO;
} else {
cell.userInteractionEnabled = YES;
//if you have to do some actions when cell is selected
}
}


Related Topics



Leave a reply



Submit