這個(gè)標(biāo)題起的好官方??
有時(shí)候,我們的cell中存在textField或其它控件,重寫(xiě)了touch方法注銷(xiāo)第一響應(yīng),卻沒(méi)有出現(xiàn)我們想要的效果,我們可以添加點(diǎn)擊手勢(shì)解決,但是textField中存在button,button的點(diǎn)擊又被手勢(shì)給吸收了,這就犯了難,如何解決呢?
當(dāng)當(dāng)當(dāng)當(dāng),答案來(lái)了!
我們可以通過(guò)重寫(xiě)tableView來(lái)實(shí)現(xiàn)!
實(shí)現(xiàn)的方法不止一種,也可以用類(lèi)別來(lái)實(shí)現(xiàn),而且,不僅可以用于tableView,也可以用于scrollView
有兩個(gè)點(diǎn)需要注意一下
-
conformsToProtocol用來(lái)檢查對(duì)象是否實(shí)現(xiàn)了指定協(xié)議類(lèi)的方法 -
respondsToSelector判斷是否有以某個(gè)名字命名的方法
寫(xiě)個(gè)代理,提供給外部使用
@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
@interface ZJD_TableView : UITableView
@property (nonatomic,weak) id<TouchTableViewDelegate> touchDelegate;
@end
設(shè)置代理屬性
@interface TouchTableView : UITableView
{
@private
id _touchDelegate;
}
@property (nonatomic,assign) id<TouchTableViewDelegate> touchDelegate;
@end
點(diǎn)擊事件重寫(xiě)
@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
使用
- (void)loadView
{
[super loadView];
TouchTableView *tableView = [[TouchTableView alloc]initWithFrame:CGRectMake(0.0, 0.0, 320, 460) style:UITableViewStyleGrouped];
tableView.touchDelegate = self;
//相關(guān)處理
[self.view addSubview:tableView];
[tableView release];
}
- (void)tableView:(TTWTableView *)tableView
touchesEnded:(NSSet *)touches
withEvent:(UIEvent *)event
{
//touch結(jié)束后的處理
}