滚动UITableView时隐藏键盘 [英] Hide keyboard when scroll UITableView
问题描述
在我的应用程序中,当我开始滚动UITableView时,我想隐藏键盘。我在互联网上搜索这个,大多数答案都是UITableView的子类(http://stackoverflow.com/questions/3499810/tapping-a-uiscrollview-to-hide-the-keyboard)。
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>
@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中不起作用
I use this class like this. But delegate function myUITableViewTouchesBegan don't work in 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];
}
我在实施方面遇到了一些麻烦:
1)myUITableViewTouchesBegan在ViewController中不起作用
2)来自MyUITableView.m的NSLog - NSLog(@delegate myUITableViewTouchesBegan); 仅当我触摸表时工作。当我开始滚动时,它是如何工作的?
我尝试覆盖scrollViewDidScroll但是comiler说MyUITableVIew可能不响应这个字符串 [super scrollViewDidScroll:scrollView];
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];
推荐答案
不确定为什么需要为此子类化UITableView。
Not sure why you need to subclass UITableView for this.
在包含普通UITableView的视图控制器中,尝试添加:
In the view controller that contains the plain UITableView, try adding this:
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
[searchBar resignFirstResponder];
}
这篇关于滚动UITableView时隐藏键盘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!