在我的应用程序中,当我开始滚动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];

推荐答案

不确定为什么需要为此为UITableView创建子类.

在包含普通UITableView的视图控制器中,try 添加以下内容:

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    [searchBar resignFirstResponder];
}

Ios相关问答推荐

2024 Beacon应用程序- startMonitoring(适用于:地区)或

如何在进行滑动删除操作时保持自定义视图背景静态

Jenkins node 无法运行fastlane命令-未找到命令

在金属中设置纹理的UV坐标以编程方式变形纹理

磁盘上的Objective-C ISA指针与实例化对象时的比较

如何在后台显示 Swift UI 中的通讯通知?

在 React Native iOS 中加载多个 JS 包

尽管存在浮点错误,如何显示正确的计算结果.计算器应用程序

SKScene 的更新功能似乎不起作用

SwiftUI 图表 BarMark 将注释对齐到图表顶部

具有线性渐变的 SVG textPath 在 Safari 中损坏

在 Swift 中使用 UITesting 捕获屏幕录像

通过函数使用 Gesture 时,键入任何视图不能符合视图

如何避免任何绑定空错误并更好地获取代码

UIButton在iOS7中没有显示突出显示

如何使用完成按钮制作 UIPickerView?

如何进行键值观察并在 UIView 的框架上获取 KVO 回调?

如何在ios中生成UUID

如何使用 presentModalViewController 创建透明视图

未找到符号:kUTTypeImage