首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在UIControlEvents上添加UITableViewCell

如何在UIControlEvents上添加UITableViewCell
EN

Stack Overflow用户
提问于 2014-02-10 22:17:01
回答 2查看 136关注 0票数 0

我想知道如何将UIControlEvent添加到UITableViewCell中?我不能在addTarget:action:forControlEvents上使用UITableViewCell方法。我不能使用didSelectCellAtIndexPath:,因为我需要知道UIControlEventTouchDownUIControlEventTouchUpInside。我怎样才能做到这一点?

谢谢!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-02-10 22:25:17

编辑:另一个选项是公开您的单元格上的UIButton属性,在cellForRowAtIndexPath中:在单元格的按钮上调用addTarget:action:forControlEvent:,传递您希望在触摸时被调用的视图控制器上的self和方法。这就排除了对委托协议的任何需要。唯一的问题是,在对单元格的按钮设置目标操作之前,请确保调用:

代码语言:javascript
复制
[cell.button removeTarget:nil 
               action:NULL 
     forControlEvents:UIControlEventAllEvents]; 

由于单元格(和它的按钮)被重用,您需要调用它,以确保您没有在按钮上堆叠目标操作。

票数 1
EN

Stack Overflow用户

发布于 2014-02-10 23:35:53

我认为最干净的解决方案是定义一个自定义UIGestureRecognizer并将其添加到UITableViewCell中。

代码语言:javascript
复制
MDGestureRec.h

#import <UIKit/UIKit.h>
#import <UIKit/UIGestureRecognizerSubclass.h>

@interface MDGestureRec : UIGestureRecognizer

- (void)reset;
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event;

@end

// ------

MDGestureRec.m

#import "MDGestureRec.h"

@implementation MDGestureRec

- (void)reset { }
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    NSLog(@"touches %@", [touches description]);
    NSLog(@"touchesBegan %@", [event description]);
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { }
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { }
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event { }

@end

// ------

    MDGestureRec *g = [[MDGestureRec alloc] init];

    [cell addGestureRecognizer:g];
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21689068

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档