關(guān)于TableView不響應(yīng)textField點(diǎn)擊事件的處理

這個(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)需要注意一下

  1. conformsToProtocol用來(lái)檢查對(duì)象是否實(shí)現(xiàn)了指定協(xié)議類(lèi)的方法
  2. 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é)束后的處理
}

摘自:http://my.oschina.net/vimfung/blog/64494

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫(kù)、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,588評(píng)論 4 61
  • 那天我無(wú)意間與先生聊天,我說(shuō)我發(fā)現(xiàn)我越來(lái)越好看了,先生看我一眼,我繼續(xù)說(shuō),我從小就是一路丑著長(zhǎng)大的。 我想起七歲那...
    林清因閱讀 1,156評(píng)論 0 0
  • 一我沒(méi)有想到的是,你月工資居然只有160元。 這是一個(gè)什么樣的連鎖店(某連鎖藥店),160元的月工資也能發(fā)放出來(lái)。...
    抱歉舊報(bào)紙閱讀 175評(píng)論 0 0
  • 你有沒(méi)有奇葩難忘的夢(mèng)里醒來(lái)的方式? 我是有,真有,這也是我要用標(biāo)題黨是方式,用一篇文章來(lái)表達(dá)的原因。我遭遇了20多...
    遛海豬666閱讀 741評(píng)論 0 1
  • 1、大胤水師統(tǒng)轄于兵部,最高指揮機(jī)構(gòu)是水師大都督府,設(shè)水師大都督一名,鄰大將軍銜,爵位視具體戰(zhàn)功而定?,F(xiàn)任水師大都...
    法癡l閱讀 683評(píng)論 0 0

友情鏈接更多精彩內(nèi)容