0

我想知道如何将 UIControlEvent 添加到 UITableViewCell?我不能addTarget:action:forControlEventsUITableViewCell. 我不能使用didSelectCellAtIndexPath:,因为我需要知道 forUIControlEventTouchDownUIControlEventTouchUpInside。我怎样才能做到这一点?

谢谢!

4

2 回答 2

1

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

[cell.button removeTarget:nil 
               action:NULL 
     forControlEvents:UIControlEventAllEvents]; 

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

于 2014-02-10T22:25:17.110 回答
0

我认为最干净的解决方案是定义一个自定义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];
于 2014-02-10T23:35:53.977 回答