在我的应用程序中,当我开始滚动UITableView时,我想隐藏键盘.我在互联网上搜索过这个问题,大多数答案都是将UITableView子类化(http://stackoverflow.com/questions/3499810/tapping-a-uiscrollview-to-hide-the-keyboard).
我制作了子类,但它不起作用.
#import <UIKit/UIKit.h>
@protocol MyUITableViewDelegate <NSObject>
@optional
- (void)myUITableViewTouchesBegan;
@end
@interface MyUITableView : UITableView <UITableViewDelegate, UIScrollViewDelegate> {
id<MyUITableViewDelegate> delegate;
}
@end
M文件
#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 {
...
我这样使用这个类.但是委托函数myUITableViewTouchesBegan在ViewController中不起作用
.h
#import <UIKit/UIKit.h>
#import "MyUITableView.h"
@interface FirstViewController : UIViewController <UITableViewDelegate, UISearchBarDelegate, MyUITableViewDelegate> {
MyUITableView *myTableView;
UISearchBar *searchBar;
}
@property(nonatomic,retain) IBOutlet MyUITableView *myTableView;
...
M
- (void) myUITableViewTouchesBegan{
NSLog(@"myUITableViewTouchesBegan");
[searchBar resignFirstResponder];
}
I have some troubles with this implemenation:
1) myUITableViewTouchesBegan dont work in ViewController
2) NSLog from MyUITableViewM - 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];