我想知道如何将UIControlEvent添加到UITableViewCell中?我不能在addTarget:action:forControlEvents上使用UITableViewCell方法。我不能使用didSelectCellAtIndexPath:,因为我需要知道UIControlEventTouchDown和UIControlEventTouchUpInside。我怎样才能做到这一点?
谢谢!
发布于 2014-02-10 22:25:17
编辑:另一个选项是公开您的单元格上的UIButton属性,在cellForRowAtIndexPath中:在单元格的按钮上调用addTarget:action:forControlEvent:,传递您希望在触摸时被调用的视图控制器上的self和方法。这就排除了对委托协议的任何需要。唯一的问题是,在对单元格的按钮设置目标操作之前,请确保调用:
[cell.button removeTarget:nil
action:NULL
forControlEvents:UIControlEventAllEvents]; 由于单元格(和它的按钮)被重用,您需要调用它,以确保您没有在按钮上堆叠目标操作。
发布于 2014-02-10 23:35:53
我认为最干净的解决方案是定义一个自定义UIGestureRecognizer并将其添加到UITableViewCell中。
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];https://stackoverflow.com/questions/21689068
复制相似问题