为实现真正的点击空白收起键盘,让UITableView响应touch事件
发布日期:2021-11-22 04:28:53 浏览次数:2 分类:技术文章

本文共 3085 字,大约阅读时间需要 10 分钟。

为了实现点击空白收起键盘,让UITableView响应touch事件,过程复杂,原因主要是为了解决uitableVIEW的视图点击或者滑动之后,键盘自动收起

做法就是重写UITableView的touch相关的方法,然后通过委托的方式提供给外部对象使用。首先定义Delegate:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
@protocol TouchTableViewDelegate <NSObject>
 
@optional
 
- (
void
)tableView:(UITableView *)tableView
     
touchesBegan:(NSSet *)touches
        
withEvent:(UIEvent *)event;
 
- (
void
)tableView:(UITableView *)tableView
 
touchesCancelled:(NSSet *)touches
        
withEvent:(UIEvent *)event;
 
- (
void
)tableView:(UITableView *)tableView
     
touchesEnded:(NSSet *)touches
        
withEvent:(UIEvent *)event;
 
- (
void
)tableView:(UITableView *)tableView
     
touchesMoved:(NSSet *)touches
        
withEvent:(UIEvent *)event;
 
 
@end

然后UITableView的子类加入一委托对象,并重写所有touch相关方法,如下:

1
2
3
4
5
6
7
8
9
@interface  TouchTableView : UITableView
{
@
private
    
id _touchDelegate;
}
 
@property (nonatomic,assign) id<TouchTableViewDelegate> touchDelegate;
 
@end

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
@implementation TouchTableView
 
@synthesize touchDelegate = _touchDelegate;
 
- (
void
)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    
[super touchesBegan:touches withEvent:event];
     
    
if
([_touchDelegate conformsToProtocol:@protocol(TouchTableViewDelegate)] &&
        
[_touchDelegate respondsToSelector:@selector(tableView:touchesBegan:withEvent:)])
    
{
        
[_touchDelegate tableView:self touchesBegan:touches withEvent:event];
    
}
}
 
- (
void
)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
    
[super touchesCancelled:touches withEvent:event];
     
    
if
([_touchDelegate conformsToProtocol:@protocol(TouchTableViewDelegate)] &&
        
[_touchDelegate respondsToSelector:@selector(tableView:touchesCancelled:withEvent:)])
    
{
        
[_touchDelegate tableView:self touchesCancelled:touches withEvent:event];
    
}
}
 
- (
void
)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    
[super touchesEnded:touches withEvent:event];
     
    
if
([_touchDelegate conformsToProtocol:@protocol(TouchTableViewDelegate)] &&
        
[_touchDelegate respondsToSelector:@selector(tableView:touchesEnded:withEvent:)])
    
{
        
[_touchDelegate tableView:self touchesEnded:touches withEvent:event];
    
}
}
 
- (
void
)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    
[super touchesMoved:touches withEvent:event];
     
    
if
([_touchDelegate conformsToProtocol:@protocol(TTWTableViewDelegate)] &&
        
[_touchDelegate respondsToSelector:@selector(tableView:touchesMoved:withEvent:)])
    
{
        
[_touchDelegate tableView:self touchesMoved:touches withEvent:event];
    
}
}
 
@end

       重写touch方法时必须把父类实现方法写上,否则UITableViewCell将无法正常工作。所有的改写工作如上所示,新的TableView类具有touch事件响应了,使用方法也很简单在原有UITableView的基础上赋予touchDelegate委托即可取到touch事件响应。如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
- (
void
)loadView
{
    
[super loadView];
    
TouchTableView *tableView = [[TouchTableView alloc]initWithFrame:CGRectMake(0.0, 0.0, 320, 460)   style:UITableViewStyleGrouped];
    
tableView.touchDelegate = self;
    
//相关处理
    
[self.view addSubview:tableView];
    
[tableView release];
}
 
 
- (
void
)tableView:(TTWTableView *)tableView
     
touchesEnded:(NSSet *)touches
        
withEvent:(UIEvent *)event
{
    
//touch结束后的处理
}

转载地址:https://blog.csdn.net/zxwcool123/article/details/50565205 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:UICollectionView如果在数据不够一屏时上下滚动
下一篇:IOS用CGContextRef画各种图形(文字、圆、直线、弧线、矩形、扇形、椭圆、三角形、圆角矩形、贝塞尔曲线、图片)

发表评论

最新留言

能坚持,总会有不一样的收获!
[***.219.124.196]2024年04月01日 07时19分14秒