UIGestureRecognizer 和 UITableViewCell 问题
我将 UISwipeGestureRecognizer
附加到 cellForRowAtIndexPath:
方法中的 UITableViewCell
,如下所示:
- (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;
}
但是,didSwipe
方法是成功滑动后总是会被调用两次。我最初认为这是因为手势开始和结束,但如果我注销gestureRecognizer本身,它们都处于“结束”状态:
-(void)didSwipe:(UIGestureRecognizer *)gestureRecognizer {
NSLog(@"did swipe called %@", gestureRecognizer);
}
控制台:
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 am attaching a UISwipeGestureRecognizer
to a UITableViewCell
in the cellForRowAtIndexPath:
method like so:
- (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:
-(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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以将手势识别器添加到
viewDidLoad
中的 tableview,而不是直接将手势识别器添加到单元格。在
didSwipe
方法中,您可以确定受影响的 IndexPath 和单元格,如下所示:Instead of adding the gesture recognizer to the cell directly, you can add it to the tableview in
viewDidLoad
.In the
didSwipe
-Method you can determine the affected IndexPath and cell as follows:它将与应用程序委托一起使用
It will work with app delegate
我遇到了同样的问题,并通过在表视图属性中勾选“滚动启用”来解决它。
我的表格视图不需要滚动,因此它不会以任何其他方式影响应用程序,但现在我在滑动手势后不会出现第一次无响应的点击。
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.
在 AwakeFromNib 方法中添加手势没有任何问题。
Adding gesture in AwakeFromNib method works with no problems.