Double tap necessary to select TableView item with Search Bar
You can add a tap gesture recognizer to your view that when fired would
- resign the search bar responder
- convert the tap location to a point in you table view
- get the cell that tap location
- 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
How to Execute a Function in a Uiviewcontroller Through a Swiftui Button
Xcode 8 Memory Graph Says "No Selection" and Not Working
How to Download a PDF File in Swift and Find in File Manager
Where to Find a Clear Explanation About Swift Alert (Uialertcontroller)
How to Set Kerning (Spacing Between Characters) on Uinavigationbar Title - Swift or Objective-C
Uiimage Aspect Fill When Using Drawinrect
How to Get Label Name from Button
Rotate a Imageview Around a Pivot Point in iOS
What am I Doing Wrong with Allowsfractionalunits on Nsdatecomponentsformatter
Hidden Property Cannot Be Changed Within an Animation Block
Change Ncwidgetdisplaymode Programmatically in iOS10 Widget
Saving Audio After Effect in iOS
Dismiss Keyboard with Swipe Gesture