UITableViewCell上面放置自定义UIView支持响应Touch事件
通过继承- (BOOL)touchesShouldCancelInContentView:(UIView *)view这个方法
- (BOOL)touchesShouldCancelInContentView:(UIView *)view{
if([view isKindOfClass:[MyTableCellView class]]){
return NO;
}
else{
return YES;
}
}
即可实现UITableViewCell上所加的MyTableCellView响应touch事件
当编辑状态下,出现可以交换顺序的控件时,如图
会导致不能编辑顺序的,就是不能移动这个Cell了
经过分析,感觉应该是自己继承那个方法导致的,经调试,发现如图的那个可拖动控件是另外一个类(私有类)_UITableViewCellGrabber
他需要返回NO,而不是默认的YES,所以上面代码做如下修改
- (BOOL)touchesShouldCancelInContentView:(UIView *)view{
if([view isKindOfClass:[MyTableCellView class]]||[view isKindOfClass:[_UITableViewCellGrabber class]]){
return NO;
}
else{
return YES;
}
}
但是这样子会导致编译通不过,提示这个类没有定义,那么就要自己来定义并扩展一下这个类
Object c中有个语法叫category
这样用
@interface ClassName ( CategoryName )
// method declarations
@end
and then implement the methods as follows:
@implementation ClassName ( CategoryName )
// method definitions
@end
使用这个语法如下代码
.h文件中
@interface _UITableViewCellGrabber : UIControl
{
}
@end
@interface _UITableViewCellGrabber ( Override )
//- (void) drawRect:(CGRect)rect;
@end
.m文件中
@implementation _UITableViewCellGrabber ( Override )
/*- (void) drawRect:(CGRect)rect
{
// overridden drawing implementation goes here
}*/
@end
这样就可以了, _UITableViewCellGrabber这个类就认出来了.
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/9059159/viewspace-592605/
,如需转载,请注明出处,否则将追究法律责任。