ios UIGestureRecognizer 和 UITableViewCell 问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4604296/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
UIGestureRecognizer and UITableViewCell issue
提问by mootymoots
I am attaching a UISwipeGestureRecognizer
to a UITableViewCell
in the cellForRowAtIndexPath:
method like so:
我在方法中将 a 附加UISwipeGestureRecognizer
到 a UITableViewCell
,cellForRowAtIndexPath:
如下所示:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
UISwipeGestureRecognizer *gesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(didSwipe:)];
gesture.direction = UISwipeGestureRecognizerDirectionRight;
[cell.contentView addGestureRecognizer:gesture];
[gesture release];
}
return cell;
}
However, the didSwipe
method is always getting called twice on successful swipe. I initially thought this was because the gesture starts and ends, but if I log out the gestureRecognizer itself, they are both in the "Ended" state:
但是,该didSwipe
方法总是在成功滑动时被调用两次。我最初认为这是因为手势开始和结束,但如果我注销gestureRecognizer 本身,它们都处于“结束”状态:
-(void)didSwipe:(UIGestureRecognizer *)gestureRecognizer {
NSLog(@"did swipe called %@", gestureRecognizer);
}
Console:
安慰:
2011-01-05 12:57:43.478 App[20752:207] did swipe called <UISwipeGestureRecognizer: 0x5982fa0; state = Ended; view = <UITableViewCellContentView 0x5982c30>; target= <(action=didSwipe:, target=<RootViewController 0x5e3e080>)>; direction = right>
2011-01-05 12:57:43.480 App[20752:207] did swipe called <UISwipeGestureRecognizer: 0x5982fa0; state = Ended; view = <UITableViewCellContentView 0x5982c30>; target= <(action=didSwipe:, target=<RootViewController 0x5e3e080>)>; direction = right>
I really really don't know why. I tried obviously checking for the Ended state, but that is no help as they both come in as "Ended" anyway... Any ideas?
我真的不知道为什么。我显然尝试检查 Ended 状态,但这无济于事,因为无论如何它们都以“Ended”形式出现......有任何想法吗?
回答by Felix
Instead of adding the gesture recognizer to the cell directly, you can add it to the tableview in viewDidLoad
.
不是直接将手势识别器添加到单元格中,您可以将其添加到viewDidLoad
.
In the didSwipe
-Method you can determine the affected IndexPath and cell as follows:
在didSwipe
-Method 中,您可以按如下方式确定受影响的 IndexPath 和单元格:
-(void)didSwipe:(UIGestureRecognizer *)gestureRecognizer {
if (gestureRecognizer.state == UIGestureRecognizerStateEnded) {
CGPoint swipeLocation = [gestureRecognizer locationInView:self.tableView];
NSIndexPath *swipedIndexPath = [self.tableView indexPathForRowAtPoint:swipeLocation];
UITableViewCell* swipedCell = [self.tableView cellForRowAtIndexPath:swipedIndexPath];
// ...
}
}
回答by Kumaresan P
It will work with app delegate
它将与应用程序委托一起使用
- (void)tableView:(UITableView*)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath
{
// code
}
回答by Mark C Mitchell
I had this same problem and solved it by ticking "Scrolling Enabled" in the table view attributes.
我有这个同样的问题,通过勾选表视图属性“滚动启用”解决了这个问题。
My table view doesn't need scrolling, so it doesn't affect the app in any other way, except now I don't get the first unresponsive tap after a swipe gesture.
我的表格视图不需要滚动,所以它不会以任何其他方式影响应用程序,除非现在我在滑动手势后没有第一次无响应点击。
回答by LembergSun
Adding gesture in AwakeFromNib method works with no problems.
在 AwakeFromNib 方法中添加手势没有问题。
class TestCell: UITableViewCell {
override func awakeFromNib() {
super.awakeFromNib()
let panGesture = UIPanGestureRecognizer(target: self,
action: #selector(gestureAction))
addGestureRecognizer(panGesture)
}
@objc func gestureAction() {
print("gesture action")
}
}