olegi olegi - 1 year ago 126
iOS Question

Hide keyboard when scroll UITableView

In my app i want hide keyboard when i start scrolling UITableView. I search about this in internet, and most answer is subclassing UITableView (http://stackoverflow.com/questions/3499810/tapping-a-uiscrollview-to-hide-the-keyboard).

I made subclass but it's dont work.

#import <UIKit/UIKit.h>

@protocol MyUITableViewDelegate <NSObject>
- (void)myUITableViewTouchesBegan;

@interface MyUITableView : UITableView <UITableViewDelegate, UIScrollViewDelegate> {
id<MyUITableViewDelegate> delegate;

.m file

#import "MyUITableView.h"

@implementation MyUITableView

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
NSLog(@"delegate scrollView"); //this is dont'work
[super scrollViewDidScroll:scrollView];

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
NSLog(@"delegate myUITableViewTouchesBegan"); // work only here
[delegate myUITableViewTouchesBegan];
[super touchesBegan:touches withEvent:event];


- (void)dealloc {

I use this class like this. But delegate function myUITableViewTouchesBegan don't work in ViewController


#import <UIKit/UIKit.h>
#import "MyUITableView.h"

@interface FirstViewController : UIViewController <UITableViewDelegate, UISearchBarDelegate, MyUITableViewDelegate> {
MyUITableView *myTableView;
UISearchBar *searchBar;

@property(nonatomic,retain) IBOutlet MyUITableView *myTableView;


- (void) myUITableViewTouchesBegan{
[searchBar resignFirstResponder];

I have some troubles with this implemenation:

1) myUITableViewTouchesBegan dont work in ViewController

2) NSLog from MyUITableView.m - NSLog(@"delegate myUITableViewTouchesBegan"); work only when i touch table. How made it's work also when i start scrolling?

I try override scrollViewDidScroll but comiler said that MyUITableVIew may be don't respond on this string [super scrollViewDidScroll:scrollView];

Answer Source

Not sure why you need to subclass UITableView for this.

In the view controller that contains the plain UITableView, try adding this:

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
    [searchBar resignFirstResponder];
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download