![](/img/trans.png)
[英]Get the size of UIButton inside a custom UITableViewCell with Autolayout
[英]Adding UILongPressGestureRecognizer to UIButton inside custom UITableViewCell
我有一個帶有各種IBOutlet的自定義單元格,但是我想在一個按鈕上添加一個UILongPressGestureRecognizer以進行長按手勢。 這是我的代碼(btw插座連接正確,按鈕的IBAction
方法正確調用):
MyCustomCell.h
@interface MyCustomCell : UITableViewCell
@property (strong, nonatomic) IBOutlet UIButton *myButton;
@property (strong, nonatomic) UILongPressGestureRecognizer *longPressGestureRecognizer;
@end
MyCustomCell.m
- (void)awakeFromNib
{
// Initialization code
self.longPressGestureRecognizer = nil;
}
MyViewController.m
#import MyCustomCell.h
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier = @"MyCell";
MyCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
if (!cell){
cell = [[MyCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
}
cell.longPressGestureRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPressGestures:)];
cell.longPressGestureRecognizer.minimumPressDuration = 1.0f;
cell.longPressGestureRecognizer.allowableMovement = 300.0f;
[cell.myButton addGestureRecognizer:cell.longPressGestureRecognizer];
}
- (void)handleLongPressGestures:(UIGestureRecognizer *)recognizer
{
if ([recognizer.view isKindOfClass:[UIButton class]]){
if (recognizer.state == UIGestureRecognizerStateBegan){
NSLog(@"Long press began");
} else if (recognizer.state = UIGestureRecognizerStateEnded){
NSLog(@"Long press ended");
}
}
}
問題是handleLongPressGestures:
永遠不會調用方法。
longPressGestureRecognizer
應該是控制器上的屬性,而不是視圖(MyCustomCell)。 將屬性移到MyViewController,然后重試。 我的猜測是,它在MyCustomCell排隊和出隊時發生了奇怪的事情。
要重用的對象(單元)應輕巧。 在這種情況下,longPressGestureRecognizer的目標是視圖控制器,並且很討厭。
嘗試這個!
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UILongPressGestureRecognizer *LongPress=[[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(handleLongPressGestures:)];
LongPress.minimumPressDuration = 1.0f;
LongPress.allowableMovement = 300.0f;
[cell.myButton addGestureRecognizer:LongPress];
}
- (void)handleLongPressGestures:(UIGestureRecognizer *)recognizer
{
if ([recognizer.view isKindOfClass:[UIButton class]]){
if (recognizer.state == UIGestureRecognizerStateBegan){
NSLog(@"Long press began");
}
else if (recognizer.state = UIGestureRecognizerStateEnded)
{
NSLog(@"Long press ended");
}
}
}
嘗試這種方式
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier = @"MyCell";
MyCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
if (!cell){
cell = [[MyCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
}
if ([[cell gestureRecognizers] count]<1) {
UILongPressGestureRecognizer *longPressGestureRecognizer;
longPressGestureRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPressGestures:)];
longPressGestureRecognizer.minimumPressDuration = 1.0f;
longPressGestureRecognizer.allowableMovement = 300.0f;
longPressGestureRecognizer.delegate = self;
[cell.myButton addGestureRecognizer:cell.longPressGestureRecognizer];
}
}
這種類型的代碼對我有用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.